我在使用mysql创建临时表时遇到了困难。
我有一个存储过程我正在创建一些sql语句我需要在我的sql中创建sql语句输出作为临时表。这是我的程序。有人帮我吗?
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `new_procedure`()
BEGIN
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(CASE WHEN workspaceid = ''',
workspaceid,
''' then "''''" ELSE NULL end) AS ',
CONCAT('`',workspaceid,'`')
)
) INTO @sql
FROM sms.hotelings;
SET @sql = CONCAT('SELECT t.Time, ', @sql, '
FROM sms.hotelings h, sms.hotelingtime t
GROUP BY t.Time');
-- PREPARE stmt FROM @sql;
-- EXECUTE stmt;
-- DEALLOCATE PREPARE stmt;
CREATE TEMPORARY TABLE IF NOT EXISTS table2 AS @sql;
select * from table2
drop temporary table table2;
END
我想使用@sql语句创建一个临时表。
提前致谢。
答案 0 :(得分:0)
如果要使用动态语句
,则必须使用PREPARESET @sql = CONCAT('CREATE TEMPORARY TABLE IF NOT EXISTS table2 AS SELECT t.Time, ', @sql, '
FROM sms.hotelings h, sms.hotelingtime t
GROUP BY t.Time');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT * FROM table2;
DROP TEMPORARY TABLE table2;