mysql select 到变量_在mysql中使用select到局部变量和准备好的语句中

bd96500e110b49cbb3cd949968f18be7.png

Everytime call this stored procedure i get this error:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL' at line 1

What seems to be the problem?

Here is the code:

DELIMITER $$

DROP PROCEDURE IF EXISTS `singledb`.`TOUR_TRANSFER`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `TOUR_TRANSFER`()

BEGIN

BEGIN -- for rubric table

DECLARE tblRubric_rubric_id INT;

SET @qry = concat('SELECT count(*) into ',tblRubric_rubric_id,' FROM zerodb2.tour_template');

PREPARE statement FROM @qry;

EXECUTE statement;

SELECT tblRubric_rubric_id;

END;

END$$

DELIMITER ;

解决方案

"Parameter markers can be used only where data values should appear, not for SQL

keywords, identifiers, and so forth."

So, we can not do:

PREPARE stmt FROM 'SELECT id INTO ? FROM t1';

We can not use ? instead of identifier (variable name).

And when you use parameter name:

PREPARE stmt FROM 'SELECT id INTO rid FROM t1';

in the string being prepared, then server simply do not know what that rid refers to in

statement you are trying to prepare. You may try to prepare it outside SP with the same

result:

mysql> prepare stmt from 'select id into p from t1';

ERROR 1327 (42000): Undeclared variable: p

So I just used user variable, see below:

DROP PROCEDURE IF EXISTS `singledb`.`TOUR_TRANSFER`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `TOUR_TRANSFER`()

BEGIN

BEGIN -- for rubric table

-- DECLARE @tblRubric_rubric_id INT;

SET @tblRubric_rubric_id := 0;

SET @qry = 'SELECT count(*) into @tblRubric_rubric_id FROM zerodb2.tour_template';

PREPARE statement FROM @qry;

EXECUTE statement;

END;

END$$

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值