读书人

一个类似循环的查询有关问题!请高手帮

发布时间: 2012-01-14 20:02:35 作者: rapoo

一个类似循环的查询问题!请高手帮忙!
需求如下:
有2个表
表1:
A B
1 11
2 22
3 33
4 44

表2:
C
1
2
3

希望结果如下:(希望能不用游标,临时表的情况下查询得到如何实现)
A C
1 1
2 1
3 1
4 1
1 2
2 2
3 2
4 2
1 3
2 3
3 3
4 3

[解决办法]
--表1:
declare @table1 table(A int,B int)
insert into @table1
select 1, 11
union select 2, 22
union select 3, 33
union select 4, 44

--表2:
declare @table2 table(C int)
insert into @table2
select 1
union select 2
union select 3

select A,C from @table1 table1,@table2 table2
[解决办法]
--表1:
declare @table1 table(A int,B int)
insert into @table1
select 1, 11
union select 2, 22
union select 3, 33
union select 4, 44

--表2:
declare @table2 table(C int)
insert into @table2
select 1
union select 2
union select 3

select a,c from @table1 cross join @table2

读书人网 >SQL Server

热点推荐