Solved

Using parameterized SQL statements with OPENROWSET in stored procedures

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SYbase 4 44
Creating a record in a database with static data from MVC to SQL (Entity) 2 39
Pivoting oracle table 9 74
Database maintenance 36 110
Creating and Managing Databases with phpMyAdmin in cPanel.
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
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…

730 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