此脚本为任何表结构生成动态 SQL。
询问:
SET NOCOUNT ON
IF OBJECT_ID (N'dbo.Table_1') IS NOT NULL
DROP TABLE dbo.Table_1
IF OBJECT_ID (N'dbo.Table_2') IS NOT NULL
DROP TABLE dbo.Table_2
CREATE TABLE dbo.Table_1 (A INT NOT NULL, B INT NOT NULL, C INT NOT NULL)
CREATE TABLE dbo.Table_2 (A INT NOT NULL, B INT NOT NULL)
INSERT INTO dbo.Table_1 (A, B, C)
VALUES (1, 1, 1), (2, 2, 2)
INSERT INTO dbo.Table_2 (A, B)
VALUES (6, 1), (8, 2)
DECLARE @SQL NVARCHAR(2000)
;WITH cte AS
(
SELECT
column_name = '[' + c.name + ']'
, table_name = '[' + s.name + '].[' + o.name + ']'
FROM sys.columns c WITH (NOLOCK)
JOIN sys.objects o WITH (NOLOCK) ON c.[object_id] = o.[object_id]
JOIN sys.schemas s WITH (NOLOCK) ON o.[schema_id] = s.[schema_id]
WHERE o.name IN ('Table_1', 'Table_2')
AND s.name = 'dbo'
AND o.[type] = 'U'
), cols AS
(
SELECT DISTINCT column_name
FROM cte
), tbl AS
(
SELECT DISTINCT table_name
FROM cte
), rs AS
(
SELECT
tbl.table_name
, column_name = ISNULL(cte.column_name, cols.column_name + ' = NULL')
FROM cols
CROSS JOIN tbl
LEFT JOIN cte ON cols.column_name = cte.column_name AND cte.table_name = tbl.table_name
), rs2 AS (
SELECT uni = ' UNION ALL' + CHAR(13) + 'SELECT ' + STUFF((
SELECT ', ' + rs.column_name
FROM rs
WHERE tbl.table_name = rs.table_name
GROUP BY rs.column_name
ORDER BY rs.column_name
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 2, '') +
' FROM ' + table_name
FROM tbl
)
SELECT @SQL = 'SELECT
' + STUFF((
SELECT CHAR(13) + ', ' + cols.column_name
FROM cols
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 2, ' ')
+ '
FROM
(' + STUFF((
SELECT CHAR(10) + uni
FROM rs2
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 11, '') + CHAR(13) +
') t'
PRINT @SQL
EXECUTE sys.sp_executesql @SQL
输出:
SELECT
[A]
, [B]
, [C]
FROM
(
SELECT [A], [B], [C] FROM [dbo].[Table_1]
UNION ALL
SELECT [A], [B], [C] = NULL FROM [dbo].[Table_2]
) t
结果:
A B C
----------- ----------- -----------
1 1 1
2 2 2
6 1 NULL
8 2 NULL