读书人

Union的有关问题

发布时间: 2012-03-18 13:55:38 作者: rapoo

Union的问题
declare @count int

Select @count=count(*) From Table1 Where id= '1 ' and Name= 'A '
if @count> 0
begin
-- Insert into @table
Select 'A ', 'AA '
end

union

Select @count=count(*) From table2 Where id= '1 ' and Name= 'B '
if @count> 0
begin
-- Insert into @table
Select 'B ', 'BB '
end

怎么把生成结果集为
A | AA
B | BB

[解决办法]
----看你的意思这样就可以了
Select 'A ', 'AA ' From Table1 Where Exists(Select 1 From Table1 Where id= '1 ' and Name= 'A ')
Union
Select 'B ', 'BB ' From Table2 Where Exists(Select 1 From Table2 Where id= '1 ' and Name= 'B ')

读书人网 >SQL Server

热点推荐