我在 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,'"');
我想我不能像字符串一样写它,因为那不起作用。
最佳答案
...
- 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 - CONCAT(MySql 存储过程)内的输出参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34244278/