[英]Error in creating temporary table in mysql
I am facing difficulty in creating temporary table in mysql. 我在mysql中创建临时表时遇到困难。
I have a stored procedure i am creating some sql statement i need to create the sql statement output as a temporary table in my sql. 我有一个存储过程,我正在创建一些sql语句,我需要将sql语句输出创建为sql中的临时表。 Here is my procedure.
这是我的程序。 any body help me please?
有人可以帮我吗?
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
I want to create a temporary table using my @sql statement. 我想使用我的@sql语句创建一个临时表。
Thanks in advance. 提前致谢。
You have to use PREPARE if you want to use a dynamic statement 如果要使用动态语句,则必须使用PREPARE
SET @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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.