質問
2つのテーブルのユニオンがありますが、これらのテーブルには列タイプがありませんが、テーブル名(または同様の識別)を返す必要があります。 Microsoft SQL Server 2008 R2を使用しています。
これが私の現在のSQLですが、それはまだタイプを返していません。
select Id, Name, CHAR(0) as Type
from Table1
union all
select Id, Name, CHAR(1) as Type
from Table2;
解決
ソリューション:
select Id, Name, 'Table_1' as Type from Table1
union all
select Id, Name, 'Table_2' as Type from Table2;
他のヒント
これはどう:
select 'Table1' as Type, Id, Name from Table1
union all select 'Table2' as type, Id, Name from Table2;
所属していません StackOverflow