--参考:
----------------------存储过程共有三种返回值-------------------
/*(一)、参数返回值@parm2;
(二)、过程返回值return 100;
(三)、数据集返回值select * from table1 where age = @parm1
请问,如何得到三类返回值?
*/
create table table1(
name varchar(30),
age integer,
primary key(name, age)
)
create procedure proc1
@parm1 integer,
@parm2 varchar(30) output
as
select @parm2 = 'zhongguo'
select * from table1 where age = @parm1
return 100
--(一)、参数返回值@parm2;
declare @re varchar(30)
exec proc1 1,@re out
select 返回结果=@re
--(二)、过程返回值return 100;
declare @re int,@parm2 varchar(30)
exec @re=proc1 1,@parm2 out
select 返回结果=@re
--(三)、数据集返回值select * from table1 where age = @parm1
--定义返回结果保存的临时表(因为是返回记录集,所以要用临时表)
--调用返回结果
--declare @re varchar(30)
--insert #re exec proc1 1,@re out
select * into #re from OPENROWSET('SQLOLEDB',
'SERVER=zhou;uid=sa;pwd=zhou;Database=pubs','SET FMTONLY OFF;SET NOCOUNT ON declare @re varchar(30) exec proc1 ''1'',@re out') as a
--显示结果
select * from #re
--处理完成后删除临时表
drop table #re
--返回数据集方式一:
/*补充*/
execute sp_executesql
N'select * from pubs.dbo.employee where job_lvl = @level',
N'@level tinyint',
@level = 35
或者多条件
execute sp_executesql
N'select * from IndividualSaleHouse where user_ID = @user_ID and Area_ID=@Area_ID ',
N'@user_ID int,@Area_ID int',
@user_ID = 1,@Area_ID=1
--返回单个值:
/* 补充 */
declare @sql nvarchar(200)
declare @cnt int
set @sql=N'select @cnt=count(*) from WQ_STINFO_B '
exec sp_executesql @sql,N'@cnt int output',@cnt output