gpt4 book ai didi

mysql - CONCAT(MySql 存储过程)内的输出参数

转载 作者:行者123 更新时间:2023-11-29 21:43:50 31 4
gpt4 key购买 nike

我在 mysql 中为“选择”作业创建了一个动态存储过程。效果还不错。但是我无法在 concat 中添加 out 参数。我怎样才能做到这一点?我尝试过:

CREATE PROCEDURE dynamic_select(IN table VARCHAR(50), IN column VARCHAR(50), IN ucolumn VARCHAR(50), OUT total INT)
BEGIN
SET @stmt = CONCAT('SELECT count(*) INTO total FROM ',table,' WHERE ',column,'="',ucolumn,'"');

我想我不能像字符串一样写它,因为那不起作用。

最佳答案

13.5.1 PREPARE Syntax

...

  • A statement prepared in stored program context cannot refer to stored procedure or function parameters or local variables because they go out of scope when the program ends and would be unavailable were the statement to be executed later outside the program. As a workaround, refer instead to user-defined variables, which also have session scope; see Section 9.4, "User-Defined Variables".
DELIMITER //

CREATE PROCEDURE `dynamic_select`(
IN `table` VARCHAR(50),
IN `column` VARCHAR(50),
IN `ucolumn` VARCHAR(50),
OUT `total` INT
)
BEGIN
SET @`stmt` := CONCAT('SELECT 1 INTO @`stmt_total`');
PREPARE `stmt2` FROM @`stmt`;
EXECUTE `stmt2`;
SET `total` := @`stmt_total`;
DEALLOCATE PREPARE `stmt2`;
END//

DELIMITER ;

SQL Fiddle demo

关于mysql - CONCAT(MySql 存储过程)内的输出参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34244278/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com