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