2

运行此代码

DECLARE @SQL VARCHAR(2500) = '''SELECT z.* from openrowset(''''SQLNCLI'''',''''Server=server;UID=user;PWD=pwd;'''', ''''SELECT distinct x.PackageName FROM [dw].[dbo].[dex] x
JOIN [dw].dbo.log l on l.executionid = x.SSISExecutionGUID '''') z''' EXECUTE (@SQL)

发出语法错误 Incorrect syntax near 'select z.* from openrowset('SQLNCLI', 'Server=server;UID=user;PWD=pw;', 'SELECT distinct x.PackageN'.

如果我从(@SQL)中删除括号,则错误将更改为: The name ''select z.* from openrowset(''SQLNCLI'', ''Server=server;UID=user;PWD=pwd;'', ''SELECT distinct x.PackageName FROM [dw].[dbo].[dex] x join [dw].dbo.log l on l.executionid = x.SSISExecutionGUID '' ) z'' is not a valid identifier.

最有趣的是,如果我将 Execute 更改为 Print,然后手动获取打印结果并将 EXECUTE 包裹起来,它会按预期工作并得到我的结果。

我认为这是一些疯狂的单引号问题,但我看不到。

有人有想法么?

4

1 回答 1

1

我只是将打印的输出粘贴到变量赋值中,它似乎可以工作(至少据我所知 - 至少它没有给出任何语法错误)

DECLARE @SQL VARCHAR(2500) = '
SELECT z.* from openrowset(''SQLNCLI'',''Server=server;UID=user;PWD=pwd;'', 
                                       ''SELECT distinct x.PackageName FROM [dw].[dbo].[dex] x
JOIN [dw].dbo.log l on l.executionid = x.SSISExecutionGUID '') z'

EXECUTE (@SQL)
于 2011-05-11T23:39:56.710 回答