MySQL将EXECUTE的结果保存在变量中? [英] MySQL save results of EXECUTE in a variable?

查看:105
本文介绍了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的结果保存在变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆