我的SQL动态查询执行并将输出放入存储过程中的变量中
我在 My sql 存储过程中生成动态查询。我想将此查询的结果放入输出参数中。如何做到这一点?
CREATE PROCEDURE 'searchInvoice'
(
OUT numOfRecords INT
)
BEGIN
DECLARE query1 TEXT;
DECLARE query2 TEXT;
SET query1 = 'SELECT COUNT(*) bla bla bla.....';
// Query1 to select the count of matching tuples..
SET query2 = 'SELECT * from bla bla bla....';
// Query2 to select original records...
// later part of this both queries generate dynamically according to some IN parameters..
// now I wanna assign the output of the query1 into numOfRecords
// and I wanna execute the query2 as well.. like this
SET @Sql = query2;
PREPARE STMT FROM @Sql;
EXECUTE STMT;
DEALLOCATE PREPARE STMT;
// output of the query2 can be read in PHP
END
如何将 query1 的输出获取到 OUT 参数(numOfRecords
) ?
I generate a dynamic query in My sql Stored procedure. I wanna get the result of this query into a out parameter. How to do this ?
CREATE PROCEDURE 'searchInvoice'
(
OUT numOfRecords INT
)
BEGIN
DECLARE query1 TEXT;
DECLARE query2 TEXT;
SET query1 = 'SELECT COUNT(*) bla bla bla.....';
// Query1 to select the count of matching tuples..
SET query2 = 'SELECT * from bla bla bla....';
// Query2 to select original records...
// later part of this both queries generate dynamically according to some IN parameters..
// now I wanna assign the output of the query1 into numOfRecords
// and I wanna execute the query2 as well.. like this
SET @Sql = query2;
PREPARE STMT FROM @Sql;
EXECUTE STMT;
DEALLOCATE PREPARE STMT;
// output of the query2 can be read in PHP
END
How to get the output of the query1 into OUT parameter(numOfRecords
) ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看这个例子 -
Have a look at this example -
您必须在存储过程中声明变量
我希望我已经理解您的问题。
You have do declare the variable within stored procedure
I hope I've understood your question.