0

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