用中文注释当查询结果的表头
效果展示
# 若存储过程存在则删除
DROP PROCEDURE IF EXISTS sel;
# 创建一个存储过程
CREATE PROCEDURE sel(db VARCHAR(21845), tab VARCHAR(21845))
# 开始内容
BEGIN
# 生成查询列注释放入 @colas 的语句
SET @s = concat('
SELECT group_concat(CASE COLUMN_COMMENT
WHEN \'\' THEN COLUMN_NAME
ELSE concat(COLUMN_NAME, \' AS \', COLUMN_COMMENT)
END SEPARATOR \', \') AS colas
INTO @colas FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = \'', db, '\' AND TABLE_NAME = \'', tab, '\'
');
# 预编译、执行、释放
PREPARE sel FROM @s;
EXECUTE sel;
DEALLOCATE PREPARE sel;
# 拼接简单查询语句
SET @s = concat('SELECT ', @colas, ' FROM ', db, '.', tab);
PREPARE sel FROM @s;
EXECUTE sel;
END;
# 查询保存的 列名 AS 注释,
SELECT @colas;
# 使用例子 sel('数据库', '表');
CALL sel('test', 'job_depend');
# 预编译的语句再执行
EXECUTE sel;
网友评论