`

MYSQL 存储过程中,使用变量名代替表名

阅读更多

在MySQL中使用变量名代替表面需要使用 prepare.(注: prepare是在 mysql 5.0以后版本才支持的. )

 

CREATE DEFINER=`offsee`@`%` PROCEDURE `pro_terminal_report`()
BEGIN   
       
        declare _version int ;
        set _version = 'terminal_log' ;
     
        set @exec_sql = concat('select * from ' , _version);
        prepare _stmt from @exec_sql ;
        execute _stmt;
        deallocate prepare _stmt;
    
END$$

DELIMITER ;

 

重点要说明的是: set @exec_sql = concat('select * from terminal_log_v' , _version);

这里的@exec_sql变量名一定是:@+变量名   , 

之前我是写成: _exec_sql总是报1064错误.

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics