输出参数可以按名称绑定,而不是按排序绑定?

时间:2014-03-12 00:46:06

标签: mysql

在下面的代码中," out b char(8)"只能绑定到" @bz:= cName,@ cz:= cSex", 我可以用名字(b)绑定它吗?

delimiter $$ 
drop procedure if exists test_out1 $$ 

create procedure test_out1(in a CHAR(1), out b char(8),out c char(1) )
begin
   select @bz:=cName,@cz:=cSex from students where cID=a;   
end $$ 
delimiter ;

call test_out1('2',@bx,@cx);
SELECT @bx,@cx  

----------------------------------------- V2

when I use 
create procedure test_out2(in a CHAR(1), out b char(8),out c char(50) )      
begin   
      select cName into b from students where cID=a; 
end $$ 
delimiter ;

- >好吧 但我用的是:

create procedure test_out2(in a CHAR(1), out b char(8),out c char(50) )
begin
     select cName into b,cSex to c from students where cID=a;   
end $$ 

- > ERROR, 您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以便在')

附近使用正确的语法

1 个答案:

答案 0 :(得分:1)

您已在程序定义中定义了OUT个参数(bc)。
您不需要将结果明确定义到其他变量,而是输出参数。

更改:

begin
   select @bz:=cName,@cz:=cSex from students where cID=a;   
end $$ 

到:

begin
   select cName, cSex into b, c from students where cID=a;   
end $$ 

执行时:

call test_out1( '2', @bx, @cx );
SELECT @bx, @cx  ;

来自过程的结果列值将分配给@bx@cx会话变量。

示例

delimiter //

CREATE PROCEDURE simpleproc (OUT dt date, OUT ts datetime)
BEGIN
   SELECT current_date, now() INTO dt, ts;
END//
delimiter ;

现在,请将其称为测试:

CALL simpleproc( @dt, @ts );
select @dt, @ts;
+------------+---------------------+
| @dt        | @ts                 |
+------------+---------------------+
| 2014-03-13 | 2014-03-13 11:19:03 |
+------------+---------------------+

参阅