日期:2014-05-17  浏览次数:20455 次

请教高手关于在存储过程里用了2个exec后在返回记录集的时候取值失败!
第一个是EXEC是执行的将1组数据插入临时表
set @sqla='insert into #temp99 select a.out_no, a.line,a.out_date FROM out_stock a'
exec @sqla
第二个是EXEC是显示内容
set @sqlb= select * FROM out_stock a,#temp99'
exec @sqlb
在不执行第一个exec的情况下程序运行正常,加了第一个后在取记录集的时候就出错
请教高手帮忙解决一下(目的是既要执行第一个exec友要第二个exec的记录集)

------解决方案--------------------

select top 0 a.out_no, a.line,a.out_date into #temp99 FROM out_stock a
set @sqla='insert into #temp99 select a.out_no, a.line,a.out_date FROM out_stock a'
exec @sqla
set @sqlb= 'select * FROM out_stock a,#temp99'
exec @sqlb

------解决方案--------------------

--就是这个意思呗,结果没错
if object_id('Tempdb..#a') is not null drop table #a
if object_id('Tempdb..#b') is not null drop table #b

create table #a(
[Id] int null,
[Title] nvarchar(100) null
)

create table #b(
[Id] int null,
[Memo] nvarchar(100) null
)
 
Insert Into #a
select 1,'a' union all
select 2,'b' union all
select 3,'c' union all
select 4,'d' union all
select 5,'e'  

select * from #a
declare @sqla nvarchar(100)
declare @sqlb nvarchar(100)
set @sqla='Insert Into #b(Id) select Id from #a '
exec(@sqla)

set @sqlb='select * from #a,#b'
exec(@sqlb)

-----------------------

(5 行受影响)
Id          Title
----------- ----------------------------------------------------------------
1           a
2           b
3           c
4           d