MySQL计数临时表的显示列名称的总行数

时间:2016-08-22 06:03:54

标签: mysql procedure temporary

在MySQL程序中,我想要从一个动态查询创建的临时表计数行,即

CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Tbl as select * from  dynamic_tbl;

描述临时tmp_Tbl; 说Field有15行,所以保存到会话变量@TotCols

再次从动态查询创建临时表: 描述临时tmp_Tbl; 说Field有4行,所以保存到会话变量@TotCols

1 个答案:

答案 0 :(得分:0)

在存储例程的正文中,请尝试:

SET @create_tmp_table_sql = CONCAT(
    'CREATE TEMPORARY TABLE IF NOT EXISTS tmp_tbl AS ',
    'SELECT * FROM dynamic_tbl'
);
PREPARE create_tmp_table_stmt FROM @create_tmp_table_sql;
EXECUTE create_tmp_table_stmt;
DEALLOCATE PREPARE create_tmp_table_stmt;

SET @TotCols = (SELECT Count(*) FROM tmp_tbl);