I'm having trouble with this query:
SELECT *
FROM OPENROWSET(
'SQLNCLI',
'DRIVER={SQL Server};',
'EXEC dbo.sProc1 @ID = ' + @id
)
Gives an error:
Incorrect syntax near '+'.
Anyone know why I'm getting this error?
I'm having trouble with this query:
SELECT *
FROM OPENROWSET(
'SQLNCLI',
'DRIVER={SQL Server};',
'EXEC dbo.sProc1 @ID = ' + @id
)
Gives an error:
Incorrect syntax near '+'.
Anyone know why I'm getting this error?
If you need parameters you can also use
sp_executesql
:OPENROWSET requires string literals, not expressions. It's complaining about the plus sign, becaue it doesn't expect anything more than a string literal and you follewed the string literal with an operator.
See http://msdn.microsoft.com/en-us/library/ms190312.aspx which states:
As suggested by Scott , you cannot use expressions in
OPENROWSET
.Try creating a dynamic sql to pass the parametersFor what it is worth.. The reason we use openrowset rather than a straight linked server query is that the processing for a linked server query happens on the local server. (Slow and often brings most of the table back)
Yes we can do the string concatination as above.
A different option where you have ease of syntax and the power of parameters.
Create a stored proc on the remote box, that proc has all the parameters you need. Call the stored proc from with a standard linked server query (same perf or better than the above soultion and significantly easier to code with.
e.g. linkedservername.database.dbo.myproc 123,'abc','someparam',getdate()
Just an option....