0

2つのテーブルの和集合があり、これらのテーブルには列タイプがありませんが、テーブル名(または同様のID)を返す必要があるため、コードでどのテーブルからのものかを知ることができます。Microsoft SQL Server2008R2を使用しています。

これが私の現在のSQLですが、まだタイプを返していません。

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

2 に答える 2

5

ソリューション:

select Id, Name, 'Table_1' as Type from Table1 
union all 
select Id, Name, 'Table_2' as Type from Table2;
于 2011-02-01T13:19:18.420 に答える
2

これはどう:

select 'Table1' as Type, Id, Name from Table1 
union all select 'Table2' as type, Id, Name from Table2;
于 2011-02-01T13:19:00.657 に答える