简体   繁体   English

有没有办法将 MySQL 中存储过程中准备好的语句返回的值存储在变量中?

[英]Is there a way to store in a variable the value returned from a prepared statement in a Stored Procedure in MySQL?

    DELIMITER  $$
    CREATE PROCEDURE prepared_return_value(IN columnName varchar(20), IN tableName varchar(20), IN rowIndex varchar(10))
    BEGIN
    SET @columnName = '';
    SET @columnName = CONCAT('Select distinct ',columnName ,' from ', tableName, ' LIMIT ', rowIndex, ',', '1', ';');
    -- SELECT @columnName;
    PREPARE stmt FROM @columnName;
    EXECUTE stmt;
    END $$
    DELIMITER ;

So, i'm trying to dynamic pivoting a table using prepared statements to return the unique value of the rows and use it to alter a table.因此,我正在尝试使用准备好的语句动态旋转表以返回行的唯一值并使用它来更改表。 But for that, i need to be able to store the returned row value, so i can use it in another prepared statement as the column name.但为此,我需要能够存储返回的行值,这样我就可以在另一个准备好的语句中使用它作为列名。 Apparently i can't store the value from the execution of prepared statement neither from a procedure.显然,我既不能存储来自过程的预准备语句的执行值,也不能存储该值。 I've tried lot of things by know, but none seemed to work, só... any hint on that?我已经尝试了很多事情,但似乎都没有工作,所以......有什么暗示吗?

The Varoable is session bound, so you can be used after the procedure has run. Varoable 是 session 绑定的,因此您可以在程序运行使用。

SELECT @columnName SELECT @columnName

direct after the running outside if the procedure.如果程序在运行后直接运行。

another option s to use an OUT Parameter besides the IN除了IN之外,另一个选项是使用OUT参数

Ypu would run Ypu 会跑

CALL return_value(columnName, tableName, rowIndex, @columnName)

And the crete procedure wil look like克里特岛程序看起来像

CREATE PROCEDURE prepared_return_value(IN columnName varchar(20), IN tableName varchar(20), IN rowIndex varchar(10), OUT myoutput TEXT

Still what you are doing is highly problematic as it could be used for sql injection, so you should white list colu,nname an Tablename你所做的仍然是一个很大的问题,因为它可以用于 sql 注入,所以你应该白名单列,命名一个表名

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何设置一个变量来存储 select 语句返回的值并在存储过程 MySQL 中执行操作? - How to set a variable to store a value returned by select statement and perform an operation in stored procedure MySQL? 如何将SELECT语句中的值存储在MySQL存储过程中的变量中? - How do I store a value from a SELECT statement in a variable in a MySQL Stored Procedure? 如何将MySQL存储过程中的选定值存储到PHP中的变量中? - How to store a selected value from a MySQL stored procedure into a variable in PHP? MYSQL从存储过程中的准备好的语句获取更新的行数 - MYSQL get row count of update from prepared statement in a stored procedure MySQL存储过程从选择语句为多个变量赋值 - MySQL store procedure Assign value to multiple variable from select statement 存储过程准备语句错误mysql php - stored procedure prepared statement bug mysql php 带预备语句的MySQL存储过程不起作用 - MySQL Stored Procedure with Prepared Statement does not work MySQL在存储过程中准备的语句不符合预期 - MySQL prepared statement in a stored procedure not behaving as expected 在准备好的语句设置其值后检查局部变量是否为 NULL 时,存储过程中出现错误 1064 - Error 1064 in Stored procedure while checking if a local variable is NULL after prepared statement has set its value 按从存储过程MYSQL中调用的函数返回的值排序 - Order by a value returned from function called in Stored Procedure MYSQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM