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

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

2012-01-14 
一个类似循环的查询问题!请高手帮忙!需求如下:有2个表表1:AB111222333444表2:C123希望结果如下:(希望能不

一个类似循环的查询问题!请高手帮忙!
需求如下:
有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

热点排行