DECLARE @Sql nvarchar(4000) = STUFF((
SELECT ' UNION ALL'+ CHAR(10) +'SELECT * FROM '+ QUOTENAME(PartName)
FROM [dbo].[Tables]
FOR XML PATH('')
), 1, 11, '')
-- When dealing with dynamic SQL, print is your best friend...
PRINT @Sql
-- Once you've visually verified the SQL looks valid, uncomment and execute.
--EXEC(@Sql)
1条答案
按热度按时间qzlgjiam1#
一点动态sql可以让您轻松实现: