在MySQL存储过程中将表作为参数发送时出错

时间:2011-07-07 14:48:04

标签: mysql stored-procedures

我写了一个商店程序。它正确执行,但是当我调用该过程时,它显示错误:

  

错误代码:1327未声明的变量:   第三

请参阅以下程序:

DELIMITER $$
USE `db_test`$$
DROP PROCEDURE IF EXISTS `test_proc`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `test_proc`(IN newsInfoTable VARCHAR(100))
BEGIN     
    SET @sql_stam = CONCAT('SELECT news INTO ', @news,' FROM ',newsInfoTable,' WHERE ',CURDATE(),'=?;');      
    PREPARE s1 FROM @sql_stam;     
    SET @where_param = DATE_FORMAT(date_time,'%Y-%m-%d');     
    EXECUTE s1 USING @where_param;    
    SELECT @news;          
 END$$
DELIMITER ;

调用参数:

USE db_test;
CALL test_proc('tbl_morning_news');

1 个答案:

答案 0 :(得分:0)

将您的代码更改为:

DELIMITER $$
USE `db_test`$$
DROP PROCEDURE IF EXISTS `test_proc`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `test_proc`(IN newsInfoTable VARCHAR(100))
BEGIN     
    SET @sql_stam = CONCAT( 'SELECT news INTO @news FROM ',newsInfoTable
                           ,' WHERE DATE(`date_time`) = CURDATE()' );      
    PREPARE s1 FROM @sql_stam;         
    SELECT @news;          
 END$$
DELIMITER ;