- 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 @[email protected]+ 'where (' + @aa + ')' select @[email protected]+ 'and (' + @bb + ')' exec(@cc)--我这样写,但是报错!if OBJECT_ID('Tempdb..#tb_zb_xq') is not null drop table #tb_zb_xqselect * 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 @[email protected]+ 'where (' + @aa + ')' select @[email protected]+ '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 @[email protected]+ 'where (' + @aa + ')' select @[email protected]+ '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 @[email protected]+ 'where (' + @aa + ')' select @[email protected]+ 'and (' + @bb + ')' insert into #tb_zb_xqexec(@cc)
------解决方案--------------------
先创建临时表 然后再insert into
insert into #tb exec(@cc)