5

I want to construct the query used with the OPENROWSET method.

Example:

SELECT *
FROM
OPENROWSET
('SQLOLEDB', 'srv'; 'login'; 'mdp';
'SELECT *
 FROM Case
 WHERE ID = ' + @caseID) 

But when I do that I get the error: Incorrect Syntax near '+'

How can I build the query? Thank

1 Answer 1

6

Although the query in OPENROWSET is specified as a string and by that means looks very much like a dynamic query, the syntax does not allow it to be constructed likewise, out of parts.

I'm afraid, you'll have to build a dynamic query, which will call OPENROWSET, something like this:

SET @sql = '
  SELECT *
  FROM
  OPENROWSET
  (''SQLOLEDB'', ''srv''; ''login''; ''mdp'';
   ''SELECT *
     FROM Case
     WHERE ID = ' + @caseID + ''')';
EXEC(@sql);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.