与 cte union all select

with cte union all select

我有两个表 ArrearsInvoices 。 我正在尝试执行以下操作:

;with Acc.. as ( select ....from ....)

select ..... from  Arrears 

UNION ALL

select ... from Invoices 

问题是结果如下:

A header B header
row row
row row

输出应该是这样的:

Table B header c header
Arrays row row
Invoices row row

您可以向每个查询添加一个字符串文字以指示table 它来自:

SELECT 'Arrays' AS table_name, *
FROM   arrays
UNION ALL
SELECT 'Invoices' AS table_name, *
FROM   invoices