如下兩個表
(A)
ID aName
---------------------------------
1 a
2 b
3 c
4 d
5 e
(B)
ID bName
---------------------------------
4 d
5 e
6 f
7 g
8 h
查詢結果
ID aName bName
---------------------------------------------------------
1 a NULL
2 b NULL
3 c NULL
4 d d
5 e e
6 NULL f
7 NULL g
8 NULL h
create table A
(
ID int primary key,
aName varchar(50)
)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
create table B
(
ID int primary key,
bName varchar(50)
)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
create procedure proc_select
as
select A.ID,A.aName,B.bName from A left join B on A.ID=b.ID
union
select B.ID,A.aName,B.bName from A right join B on A.ID=B.ID
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
insert into A values(1,'a')
insert into A values(2,'b')
insert into A values(3,'c')
insert into A values(4,'d')
insert into A values(5,'e')
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
insert into B values(4,'d')
insert into B values(5,'e')
insert into B values(6,'f')
insert into B values(7,'g')
insert into B values(8,'h')
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
(A)
ID aName
---------------------------------
1 a
2 b
3 c
4 d
5 e
(B)
ID bName
---------------------------------
4 d
5 e
6 f
7 g
8 h
查詢結果
ID aName bName
---------------------------------------------------------
1 a NULL
2 b NULL
3 c NULL
4 d d
5 e e
6 NULL f
7 NULL g
8 NULL h
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)