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

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

要回复问题请先登录注册