SQL: OPENROWSET, can't build for the request string?

前端 未结 1 1929
遇见更好的自我
遇见更好的自我 2020-12-11 06:20

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

Example:

SELECT *
FROM
OPENROWSET
(\'SQLOLEDB\', \'srv\'; \'login\'; \'mdp\';
\'SELECT         


        
相关标签:
1条回答
  • 2020-12-11 07:07

    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);
    
    0 讨论(0)
提交回复
热议问题