提问人:wielebny 提问时间:3/29/2022 最后编辑:wielebny 更新时间:3/29/2022 访问量:300
链接服务器、结果查询xp_cmdshell到表的迁移 SQL Server 2005 到 SQL Server 2019
Linked servers, results query xp_cmdshell to table migration SQL Server 2005 to SQL Server 2019
问:
我正在将过程从 SQL Server 2005 Microsoft迁移到 SQL Server 2019 Microsoft,但在尝试将链接服务器中的查询结果xp_cmdshell插入表时卡住了
我没有想法了
Microsoft SQL Server 2005 中的旧解决方案:
INSERT INTO LOGTABLE (ShopNo,Line) SELECT 1, OUTPUT FROM openquery ([IP_LINKED_SERV],'set fmtonly off; exec master..xp_cmdshell ''type d:\log\file.log'' ')
Microsoft SQL Server 2019 给我错误:
消息 11519,级别 16,状态 1,过程
sp_describe_first_result_set,第 1 行 [批处理启动行 0] 无法确定元数据,因为语句“exec master..xp_cmdshell“键入 d:\log\file.log”将调用扩展存储过程。
我找到了一种如何在链接服务器上在 SQL Server 2019 中执行xp_cmdshell的方法
EXEC ('set fmtonly off;exec master..xp_cmdshell ''type d:\log\file.log'' ') AT [IP_LINKED_SERV]
但是,我无法将此结果插入表中
INSERT INTO LOGTABLE (ShopNo,Line) SELECT '998', OUTPUT FROM EXEC ('set fmtonly off;exec master..xp_cmdshell ''type d:\log\file.log'' ') AT [IP_LINKED_SERV]
关键字“EXEC”附近的语法不正确。
SQL2005 中过程的一部分:
DECLARE TableCursor CURSOR FOR
SELECT IP, SqlUser, SqlPass, Object FROM ..ObjectInfo
OPEN TableCursor
FETCH NEXT FROM TableCursor INTO @Ip, @SqlUser, @SqlPass, @Object
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @Object
SELECT @PARAMS = ' @tmp_object varchar(5) OUTPUT'
set @SQL = 'INSERT INTO LOGTABLE (Object,Line) SELECT @tmp_object, output FROM openquery (['+@Ip+'],''set fmtonly off;
exec master..xp_cmdshell ''''type d:\log\file.log''''
'')'
BEGIN TRY
EXECUTE sp_executesql @SQL,@PARAMS, @tmp_object = @Object OUTPUT
END TRY
BEGIN CATCH
INSERT INTO LOGTABLE (Object, Line) VALUES(@Object, '-error')
END CATCH```
答: 暂无答案
评论
INSERT INTO... EXEC
INSERT INTO ... SELECT ... FROM EXEC
xp_cmdshell
INSERT
IP_LINKED_SERV.master.sys.xp_cmdshell
xp_cmdhsell