两种方法:
1.
select * into #t from OPENROWSET('SQLOLEDB','SERVER=servername;uid=sa;pwd=123;Database=testdb','SET FMTONLY OFF;set nocount on;exec sp2 参数') as a
select * from #t
drop table #t2.
create table #t(...)
insert #t exec sp2 参数
select * from #t
drop table #t
1.
select * into #t from OPENROWSET('SQLOLEDB','SERVER=servername;uid=sa;pwd=123;Database=testdb','SET FMTONLY OFF;set nocount on;exec sp2 参数') as a
select * from #t
drop table #t2.
create table #t(...)
insert #t exec sp2 参数
select * from #t
drop table #t
insert into #tmp exec sp_who
要执行这一句,前提必须知道sp_who的结果集的表结构。
这样的语句又不能运行:select * into #tmp from exec sp_who解答:
1:
select * into #z from OPENROWSET(
'SQLOLEDB',
'SERVER=server;uid=sa;pwd=sapwd;Database=master','exec sp_who') as a2: 如果存储过程中用到临时表,要用set fmtonly off
select * into #z from OPENROWSET(
'SQLOLEDB',
'SERVER=server;uid=sa;pwd=sapwd;Database=master','SET FMTONLY OFF; exec 存储过程名') as a
/*(一)、参数返回值@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