日期:2014-05-18  浏览次数:20558 次

查询的数据存入临时表问题!
SQL code

--这是我条用函数查询的所需数据,我想把它再放入临时表#tb_zb_xq里,怎么放呢?
declare @aa varchar(8000)
declare @bb varchar(8000)
declare @cc varchar(8000)
select @aa= dbo.GetLikeOrStr3('dzkdm', '01',';')
select @bb= dbo.GetLikeOrStr3('ysdwdm', '117001',';')
select @cc= 'select * from #tb_zb ' 
select @cc=@cc+ 'where (' + @aa + ')' 
select @cc=@cc+ 'and (' + @bb + ')' 
exec(@cc)

--我这样写,但是报错!
if OBJECT_ID('Tempdb..#tb_zb_xq') is not null
  drop table #tb_zb_xq

select * into #tb_zb_xq from(
declare @aa varchar(8000)
declare @bb varchar(8000)
declare @cc varchar(8000)
select @aa= dbo.GetLikeOrStr3('dzkdm', '01',';')
select @bb= dbo.GetLikeOrStr3('ysdwdm', '117001',';')
select @cc= 'select * from #tb_zb ' 
select @cc=@cc+ 'where (' + @aa + ')' 
select @cc=@cc+ 'and (' + @bb + ')' 
exec(@cc)
) t

--求指点!!!



------解决方案--------------------
SQL code

create table #tb(/*动态执行的结果集所有列*/)

declare @aa varchar(8000)
declare @bb varchar(8000)
declare @cc varchar(8000)
select @aa= dbo.GetLikeOrStr3('dzkdm', '01',';')
select @bb= dbo.GetLikeOrStr3('ysdwdm', '117001',';')
select @cc= 'select * from #tb_zb ' 
select @cc=@cc+ 'where (' + @aa + ')' 
select @cc=@cc+ 'and (' + @bb + ')' 
insert into #tb exec(@cc)

------解决方案--------------------
SQL code
select * into #tb_zb_xq from #tb_zb where 1=0  --拷贝表结构

declare @aa varchar(8000)
declare @bb varchar(8000)
declare @cc varchar(8000)
select @aa= dbo.GetLikeOrStr3('dzkdm', '01',';')
select @bb= dbo.GetLikeOrStr3('ysdwdm', '117001',';')
select @cc= 'select * from #tb_zb ' 
select @cc=@cc+ 'where (' + @aa + ')' 
select @cc=@cc+ 'and (' + @bb + ')' 

insert into #tb_zb_xq
exec(@cc)

------解决方案--------------------
先创建临时表 然后再insert into

insert into #tb exec(@cc)