Running this code
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)
Issues a syntax error Incorrect syntax near 'select z.*
from openrowset('SQLNCLI', 'Server=server;UID=user;PWD=pw;',
'SELECT distinct x.PackageN'.
if I remove the parens from the (@SQL) the error changes to this:
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.
and funniest of all, if i change Execute to Print, then manually take the printed result and wrap EXECUTE around it, it works as expected and gets me my results.
I'm thinking it's some crazy single quote issue, but I can't see it.
Anyone have any ideas?