首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

一次查询,把结果放入两个临时表解决思路

2012-06-15 
一次查询,把结果放入两个临时表一个表,A B C三列可以这样把查询结果放到两个临时表里select * into #t1 fr

一次查询,把结果放入两个临时表
一个表,
A B C三列

可以这样把查询结果放到两个临时表里

select * into #t1 from tb where C=1 and B is null
select * into #t2 from tb where C=1 and A is null

这样要查两次,当然也可以这样

select * into #t from tb where c=1;
select * into #t1 from #t where b is null
select * into #t2 from #t where a is null
这样的话多了一次into表,into占整个查询所占开销的比例也不小。

有没有什么好办法
像这样
select * 
(when case b is null then into #t1 else into #t2 end) from tb where c=1
但不知道上面这个我想出来的代码可行不可行

[解决办法]

SQL code
declare @table table (a int,b int,c int)insert into @tableselect null,null,1 union allselect 1,null,1 union allselect null,1,1 union allselect 2,2,1select * from @table/*a           b           c----------- ----------- -----------NULL        NULL        1   --插入#t1和#t21           NULL        1   --插入#t2NULL        1           1   --插入#t12           2           1   --不满足条件不要*/--确认楼主是不是这个意思?
[解决办法]
还是分开进行吧。
[解决办法]
select * into #t1 from tb where C=1 and (B is null or A is null)
或者
select * into #t1 from
(
select * from tb where C=1 and B is null 
union
select * from tb where C=1 and A is null 
) as a

热点排行