我试图从不同的SQL服务器获取数据,然后将它们插入到临时表中,然后使用SQLCMD处理数据。
我有这个过程的代码,如果你只有一个数据库,那就完美了:
DECLARE @main table(Id int identity(1,1), Country nvarchar(5), [Message] nvarchar(200), Date datetime, pcname nvarchar(200))
:connect Plant1
use Plant1
GO
INSERT INTO @main
SELECT 'Plant1', Message, date, pcname FROM log.easyhmi WHERE DATE > GETDATE() -15 AND [Message] LIKE 'MakeChangeOver | Verify changeover runinng processInstance Id: %'ORDER BY 1 DESC
GO
:connect Plant2
use Plant2
GO
INSERT INTO @main
SELECT 'Plant2', Message, date, pcname FROM log.easyhmi WHERE DATE > GETDATE() -15 AND [Message] LIKE 'MakeChangeOver | Verify changeover runinng processInstance Id: %'ORDER BY 1 DESC
GO
:connect Plant3
use Plant3
GO
INSERT INTO @main
SELECT 'Plant3', Message, date, pcname FROM log.easyhmi WHERE DATE > GETDATE() -15 AND [Message] LIKE 'MakeChangeOver | Verify changeover runinng processInstance Id: %'ORDER BY 1 DESC
GO
--Process data here......
但如果我添加更多数据库,则无法找到@main
。
有没有办法可以做到这一点?