在SQL中的表联合中返回表类型

new*_*bie 0 sql union-all

我有两个表的联合,这些表没有列类型,但我需要返回表名(或类似的标识),所以我可以在我的代码中知道它来自哪个表.我正在使用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;
Run Code Online (Sandbox Code Playgroud)

Har*_*OTA 5

解决方案:

select Id, Name, 'Table_1' as Type from Table1 
union all 
select Id, Name, 'Table_2' as Type from Table2;
Run Code Online (Sandbox Code Playgroud)