SQL存储过程返回值

来源:互联网 发布:带帽子女孩网络歌手 编辑:程序博客网 时间:2024/05/16 10:17

先靠一下自己。

      当我做多了ORACLE时,我就忘了SQL的储存过程可以返回数据集这个事实了。

     。。。。。。。。。。。。。。。。

Create procedure  test

     @t1 int,

     @t2 nvarchar(200) out

as

    set t2='这个是输出参数';

go

 

调用

 declare @out_t2   nvarchar(200);

exec test 1,@out_t2;

select @out_t2;

输出:

 这个是输出参数

------

也可以定义一个接收参数,接收存储过程的成功与否的默认返回值(这会是一个整数,0 是无错误执行,其它数为错误代码!)

declare @val  int;

declare @out_t2   nvarchar(200);

exec @val = test 1,@out_t2;

select @out_t2,@val;

除了这些简单参数,存储过程还可以直接返回一个数据集

 

create table table_2( i_key int,i_value varchar(20));
declare @d int;
set @d=1;
WHILE @d<10
begin
 insert into table_2 select @d+1,cast(@d as varchar(50))+'S';
 set @d = @d+1;
end;

Create procedure  test

     @t1 int

as

    select * from table_2;

go

执行:

exec test 1;

结果,就是table_2表的集合.

 

 

 

原创粉丝点击