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

Union的有关问题

2012-03-18 
Union的问题declare@countintSelect@countcount(*)FromTable1Whereid 1andName Aif@count 0begin--Ins

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 ')

热点排行