Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 771
FRO Avatar asked Aug 04 '11 02:08

FRO


People also ask

How to use OPENROWSET in SQL Server?

The OPENROWSET function can be referenced in the FROM clause of a query as if it were a table name. The OPENROWSET function can also be referenced as the target table of an INSERT , UPDATE , or DELETE statement, subject to the capabilities of the OLE DB provider.

What is Openrowset function?

The OPENROWSET(BULK...) function allows you to access files in Azure Storage. OPENROWSET function reads content of a remote data source (for example file) and returns the content as a set of rows.


1 Answers

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);
like image 155
Andriy M Avatar answered Sep 27 '22 15:09

Andriy M