概述
无无聊聊写了一个mysql动态获取中文字段名的存储过程
还有许多要优化的地方
CREATE PROCEDURE P_TABLE_NAME_CHS
(
IN v_table_name
VARCHAR(100)
)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ‘’
BEGIN
DECLARE vstrSql VARCHAR(5000) ;
set @num:=1;
set @v_column_name=’’;
select COUNT(1) into @count from information_schema.columns where table_name=v_table_name and ordinal_position=@num;
#SELECT @COUNT;
WHILE @count>0 do
select COUNT(1) into @count from information_schema.columns where table_name=v_table_name and ordinal_position=@num;
select column_name into @v_column_name_temp from information_schema.columns where table_name=v_table_name and ordinal_position=@num;
select column_comment into @V_column_comment from information_schema.columns where table_name=v_table_name and ordinal_position=@num;
SET @v_column_name_temp:=CONCAT(@v_column_name_temp,’ as ‘’,@V_column_comment,’’’);
set @v_column_name:=concat(@v_column_name,’,’,@v_column_name_temp);
#SELECT @v_column_name;
set @num:=@num+1;
select COUNT(1) into @count from information_schema.columns where table_name=v_table_name and ordinal_position=@num;
end WHILE;
set @v_column_name:=substr(@v_column_name,2,length(@v_column_name));
#select @v_column_name;
SET vstrSql = CONCAT(
'SELECT ‘,@v_column_name,’ FROM ',v_table_name
);
SELECT vstrSql;
SET @vSql = vstrSql;
#预处理需要执行的动态SQL,其中stmt是一个变量
PREPARE stmt
FROM
@vSql;
#执行语句
EXECUTE stmt;
#释放语句
DEALLOCATE PREPARE stmt;
end;
最后
以上就是天真鸡翅为你收集整理的mysql动态获取中文字段名的全部内容,希望文章能够帮你解决mysql动态获取中文字段名所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复