SQL 中循环查询到三个结果集(查询的字段都是一样的) 如何将三个结果集中的数据到一个里面去

最好有具体示例 写一下 谢谢

数据不多就直接放到临时表中,如:
declare @counts int
create table #temp(字段)

set @counts =0
while @counts <(select count(*) from 表)
begin
insert into #temp
select * from 表where各种条件
set @counts=@counts+1

end
select * from #temp

go
如果数据量较大,创建一个实体表,加上必要的索引以便查询
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-06-28
select * from A
UNION ALL
SELECT * FROM B
UNION ALL
SELECT * FROM C

???追问

declare @counts int
set @counts =0
while @counts <(select count(*) from 表)
begin
select * from 表where各种条件
end
go

这种

追答

你那个count是控制循环次数,没有办法的

追问

什么意思?

第2个回答  推荐于2018-04-23
select * from 结果集1
union
select * from 结果集2
union
select * from 结果集3

没看到楼上的回答,你的那个循环是死循环,永久执行,最起码得加一个 set @count=@count+1
不过你这个循环好像没啥意义。
相似回答