Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有两个表的联合,这些表没有列类型,但我需要返回表名(或类似的标识),以便我可以在我的代码中知道它来自哪个表。我正在使用 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;