如何从准备好的语句中获取标量结果?
是否可以将准备好的语句的结果设置为变量?我正在尝试创建以下存储过程,但失败了:
第 31 行出现错误 1064 (42000):您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在 'stmt USING @m、@c、@a; 附近使用的正确语法
DROP PROCEDURE IF EXISTS deleteAction;
DELIMITER $$
CREATE PROCEDURE deleteAction(
IN modul CHAR(64),
IN controller CHAR(64),
IN actn CHAR(64))
MODIFIES SQL DATA
BEGIN
PREPARE stmt FROM 'SELECT id
FROM actions
WHERE `module` = ?
AND `controller` = ?
AND `action` = ?';
SET @m = modul;
SET @c = controller;
SET @a = actn;
SET @i = EXECUTE stmt USING @m, @c, @a;
DEALLOCATE PREPARE stmt;
DELETE FROM acl WHERE action_id = @i;
DELETE FROM actions WHERE id = @i;
END
$$
DELIMITER ;
Is it possible to set the result from a prepared statement into a variable? I am trying to create the following stored procedure but it is failing:
ERROR 1064 (42000) at line 31: 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 'stmt USING @m, @c, @a;
DROP PROCEDURE IF EXISTS deleteAction;
DELIMITER $
CREATE PROCEDURE deleteAction(
IN modul CHAR(64),
IN controller CHAR(64),
IN actn CHAR(64))
MODIFIES SQL DATA
BEGIN
PREPARE stmt FROM 'SELECT id
FROM actions
WHERE `module` = ?
AND `controller` = ?
AND `action` = ?';
SET @m = modul;
SET @c = controller;
SET @a = actn;
SET @i = EXECUTE stmt USING @m, @c, @a;
DEALLOCATE PREPARE stmt;
DELETE FROM acl WHERE action_id = @i;
DELETE FROM actions WHERE id = @i;
END
$
DELIMITER ;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可能看起来很奇怪,但您可以直接在准备好的语句字符串中分配变量:
测试用例:
结果:
It may seem strange, but you can assign the variable directly in the prepared statement string:
Test case:
Result:
使用此代码
Use this code
甚至不确定为什么在示例中使用动态 sql - 这看起来简单得多
not even sure why you're using dynamic sql in your example - this seems a lot simpler