Solved

Using parameterized SQL statements with OPENROWSET in stored procedures

Posted on 2002-04-12
2
551 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In today’s complex data management environments, it is not unusual for UNIX servers to be dedicated to a particular department, purpose, or database.  As a result, a SAS® data analyst often works with multiple servers, each with its own data storage…
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now