2011-02-01 45 views
0

我有兩個表的聯合,這些表沒有列類型,但我需要返回表名(或類似的標識),所以我可以在我的代碼中知道它是哪張表從。我正在使用Microsoft SQL Server 2008 R2。在SQL表中返回表類型

這是我目前的SQL,它還沒有返回類型。

select Id, Name, CHAR(0) as Type 
    from Table1 
union all 
select Id, Name, CHAR(1) as Type 
    from Table2; 
+0

選擇ID,名稱, '表1' 作爲TABLETYPE從表1 UNION ALL SELECT標識,名稱, '表2' 作爲表2的表格類型 – Arvo 2011-02-01 13:19:08

回答

5

解決辦法:

select Id, Name, 'Table_1' as Type from Table1 
union all 
select Id, Name, 'Table_2' as Type from Table2; 
+0

Thanx爲正確的答案! – newbie 2011-02-01 13:27:04

2

如何:

select 'Table1' as Type, Id, Name from Table1 
union all select 'Table2' as type, Id, Name from Table2; 
+0

Thanx爲正確答案! – newbie 2011-02-01 13:27:34