mysql创建存储过程动态SQL语句

DROP PROCEDURE IF EXISTS x.`wk`;
DELIMITER $$
CREATE PROCEDURE `x`.`wk`()
BEGIN
    DECLARE crs VARCHAR ( 500 );
    DECLARE t VARCHAR ( 50 );

    SET t = CONCAT( "t_track_recording_", YEAR ( CURRENT_DATE ) );
    SET crs = CONCAT( "CREATE TABLE IF NOT EXISTS ", t, " like t_track_recording; " );
    
    SET @sql = crs;
    PREPARE stmt FROM @sql;         -- 预处理动态sql语句
    EXECUTE stmt ;                        -- 执行sql语句
    DEALLOCATE PREPARE stmt;      -- 释放prepare    
    
END$$

CALL `x`.`wk`()

 记得一定要定义delimiter $$

posted @ 2019-11-05 10:16  屌丝大叔的笔记  阅读(5785)  评论(4编辑  收藏  举报