34,576
社区成员
发帖
与我相关
我的任务
分享
--> By dobear_0922(小熊) 2008-12-17 13:31:28
--> 测试数据:[A]
create table [A]([SCQ_ID] int,[SCQ_Title] varchar(1),[Range] varchar(2))
insert [A]
select 1,'A','10' union all
select 2,'B','09' union all
select 3,'C','10' union all
select 4,'D','08'
--> 测试数据:[B]
create table [B]([MCQ_ID] int,[MCQ_Title] varchar(2),[Range] varchar(2))
insert [B]
select 1,'A1','09' union all
select 2,'B1','09' union all
select 3,'C1','10' union all
select 4,'D1','08'
--> 测试数据:[C]
create table [C]([LCQ_ID] int,[LCQ_Title] varchar(2),[Range] varchar(2))
insert [C]
select 1,'A2','07' union all
select 2,'B2','09' union all
select 3,'C2','08' union all
select 4,'D2','08'
--> 目标数据:[D]
create table D(Range_ID int identity(1,1), [Range] varchar(2))
go
create proc p1 as
begin
truncate table D
insert D select [Range] from A
union select [Range] from B
union select [Range] from C order by [Range]
end
GO
exec p1
select * from D
/*
Range_ID Range
----------- -----
1 07
2 08
3 09
4 10
(4 行受影响)
*/
drop proc p1
drop table A,B,C,D
create proc p1 as
begin
insert [目标表] select Range from A表
union select Range from B表
union select Range from C表
end
GO
SELECT DISTINCT * FROM (
SELECT * FROM TA
UNION ALL
SELECT * FROM TB
UNION ALL
SELECT * FROM TC)t