sqlserver 带返回值的存储过程

 1 create proc test
 2     @result nvarchar(25) output
 3 as
 4 begin 
 5     select @result = 'haha'
 6     return 1;
 7 end
 8 go
 9 
10 declare @result varchar(25),
11         @count int
12 exec @count = test @result output
13 print @result  --查询输出参数
14 print @count  --查询返回值

执行上面一段代码, 可以得到 @result = 'haha', @count = 1

存过过程中的return语句只能返回int类型值, 而输出类型可以返回任意你想要的类型

 

**用SQL语句获得一个存储过程返回的表

create proc [dbo].[test1]
  @id int
as
begin
    select 1 as id,'abc' as name union all 
    select @id as id,'zzz' as name
end
GO
declare   @table   table(id   int,name varchar(50))--定义表变量来存放存储过程返回的内容
insert  into @table exec test1 2--将存储过程执行的结果放入表变量中
select * from @table    --查看表变量中的结果

 


 

posted on 2014-06-12 21:48  Stomach_ache  阅读(571)  评论(0编辑  收藏  举报

导航