Solved

Using parameterized SQL statements with OPENROWSET in stored procedures

Posted on 2002-04-12
2
575 Views
Last Modified: 2012-06-27
I am trying to copy data from one SQL Server database to another, on separate servers, on a daily basis.  I have had success already moving data between servers with the following command:

SELECT * INTO DestServerTableName FROM OPENROWSET ('SQLOLEDB', 'SrcServerName';'SrcServerLogin';'SrcServerPassword',
'SELECT * FROM SrcDatabase.dbo.SrcTableName WHERE Business_Date = ''11/2/2001''')

The problem arises, however, when I want to use not a hard-coded parameter, but a variable parameter in my SQL statement.  I tried the following:

DECLARE @BusDt DATETIME
SELECT @BusDt = BUSINESS_DATE FROM DateDbaseName..DateTable

DECLARE @StrSql as NVARCHAR(1000)
SET @StrSql = 'SELECT * FROM SrcDatabase.dbo.SrcTableName WHERE Business_Date = ''11/2/2001'''

SELECT * INTO DestServerTableName FROM OPENROWSET ('SQLOLEDB', 'SrcServerName';'SrcServerLogin';'SrcServerPassword',
@StrSql)

This produces the error "Line 2: Incorrect syntax near '@StrSQL'."  I tried various permutations of this (declaring @StrSql as CHAR or VARCHAR or as 500 char long instead of 1000, omitting the WHERE clause from the select statement I stored in @StrSql to just see if I could get a variable to work in the OPENROWSET command), without success.  Microsoft documentation doesn't tell me the variable type of the SQL statement parameter, so I'm not sure what else to try.  Thanks in advance for any help you can give!

0
Comment
Question by:shammann
  • 2
2 Comments
 
LVL 5

Expert Comment

by:spcmnspff
ID: 6936957
Here's a dynamic sql version that will work for you.  This works by setting the quoted identifier off in your dynamic connection and using a combination of single and double quotes to embed your parameter's value in the dynamic sql.
I would comment the exec statement out and uncomment the print command at the bottom first to see what the dynamic sql looks like.  After this restore the code and run it.  Good luck . . .

Declare @SQL VarChar(500)

Declare @BusDt DateTime
Set @BusDt = '1/1/2002 3:00:00'

Set @SQL = 'Set Set Quoted_Identifier Off' + Char(13) +
'SELECT * INTO DestServerTableName FROM OPENROWSET ('+ Char(34) + 'SQLOLEDB' +
Char(34) + ',' + Char(34) + 'SrcServerName' + Char(34) + ';' + Char(34) + 'SrcServerLogin' + Char(34) +  ';' + Char(34) + 'SrcServerPassword' + Char(34) + ',' + Char(34) +
'SELECT * FROM SrcDatabase.dbo.SrcTableName WHERE Business_Date = '
+ Char(39) + Convert(VarChar,@BusDt) + Char(39) + Char(34) + ')'

--Print @SQL
Exec(@SQL)
0
 
LVL 5

Accepted Solution

by:
spcmnspff earned 50 total points
ID: 6937026
Try this one instead.  Somehow I ended up with two set statements next to each other . . . =)

Declare @SQL VarChar(500)

Declare @BusDt DateTime
Set @BusDt = '1/1/2002 3:00:00'

Set @SQL = 'Set Quoted_Identifier Off' + Char(13) +
'SELECT * INTO DestServerTableName FROM OPENROWSET ('+ Char(34) + 'SQLOLEDB' +
Char(34) + ',' + Char(34) + 'SrcServerName' + Char(34) + ';' + Char(34) + 'SrcServerLogin' + Char(34)
+  ';' + Char(34) + 'SrcServerPassword' + Char(34) + ',' + Char(34) +
'SELECT * FROM SrcDatabase.dbo.SrcTableName WHERE Business_Date = '
+ Char(39) + Convert(VarChar,@BusDt) + Char(39) + Char(34) + ')'

--Print @SQL
Exec(@SQL)
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some very basic things about SQL Server filegroups.
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question