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 $$


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM