CONCAT 中的输出参数(MySql 存储过程)
Out parameter inside CONCAT (MySql Stored Procedures)
我在 mysql 中为 "select" 作业创建了一个动态存储过程。它工作正常。但是我不能在 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,'"');
我想我不能把它写成字符串,因为那行不通。
...
- 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 ;
我在 mysql 中为 "select" 作业创建了一个动态存储过程。它工作正常。但是我不能在 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,'"');
我想我不能把它写成字符串,因为那行不通。
...
- 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 ;