[英]Perform Union if table with these names exist
I have attendance database where every month new table get generated like TRNS0419,TRNS0519,TRNS0619,TRNS0719
.To combine data i have used Union. 我有考勤数据库,每个月都会生成新表,如
TRNS0419,TRNS0519,TRNS0619,TRNS0719
。要合并数据,我使用了Union。 But every month i have enter table name manually, Is there any way if automaticalls picks data when ever new table gets generated such TRNS0819 但是每个月我都会手动输入表名,如果生成新表时,automaticalls是否会选择数据,有什么办法吗,例如TRNS0819
I have tried using union all but its isnt taking table which is not present. 我试过使用工会,但它不是现成的餐桌。
Select * from TRNS0419 union all Select * from TRNS0519
union all Select * from TRNS0619 union all Select * from TRNS0719
my query is not taking union all Select * from TRNS0819
because this isn't available in db 我的查询未
union all Select * from TRNS0819
因为这在db中不可用
It should combine all tables and show result in find temp table. 它应该合并所有表并在find temp表中显示结果。 Please help
请帮忙
Wrap the following code in stored procedure: 在存储过程中包装以下代码:
DECLARE @DynamicTSQLStatement NVARCHAR(MAX);
SELECT @DynamicTSQLStatement = STUFF
(
(
SELECT N' UNION ALL SELECT * FROM ' + '[' + SCHEMA_NAME([schema_id]) + '].[' + [name] + ']'
FROM [sys].[tables]
WHERE [name] LIKE 'TRNS%'
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1
,10
,''
);
EXEC sp_executesql @DynamicTSQLStatement;
You can add more filters when table name is extracted from the [sys].[tables]
view. 从
[sys].[tables]
视图中提取表名时,可以添加更多过滤器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.