MySQL save results of EXECUTE in a variable?(MySQL 将 EXECUTE 的结果保存在变量中?)
问题描述
如何将 EXECUTE 语句的结果保存到变量中?类似的东西
How do I save the results of EXECUTE statement to a variable? Something like
SET a = (EXECUTE stmtl);
推荐答案
如果你想用准备好的语句来做到这一点,那么你需要在原始语句声明中包含变量赋值.
If you want to do this with a prepared statement, then you need to include the variable assignment in the original statement declaration.
如果您想使用存储的例程,那就更容易了.可以将存储函数的返回值直接赋值给变量,存储过程支持输出参数.
If you want to use a stored routine it's easier. You can assign the return value of a stored function directly to a variable, and stored procedures support out parameters.
示例:
准备好的声明:
PREPARE square_stmt from 'select pow(?,2) into @outvar';
set @invar = 1;
execute square_stmt using @invar;
select @outvar;
+---------+
| @outvar |
+---------+
| 1 |
+---------+
DEALLOCATE PREPARE square_stmt;
存储函数:
delimiter $$
create function square_func(p_input int) returns int
begin
return pow(p_input,2);
end $$
delimiter ;
set @outvar = square_func(2);
select @outvar;
+---------+
| @outvar |
+---------+
| 4 |
+---------+
存储过程:
delimiter $$
create procedure square_proc(p_input int, p_output int)
begin
set p_output = pow(p_input,2);
end $$
delimiter ;
set @outvar = square_func(3);
call square_proc(2,@outvar);
select @outvar;
+---------+
| @outvar |
+---------+
| 9 |
+---------+
这篇关于MySQL 将 EXECUTE 的结果保存在变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MySQL 将 EXECUTE 的结果保存在变量中?


基础教程推荐
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- oracle区分大小写的原因? 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- 在多列上分布任意行 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01