Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using parameterized SQL statements with OPENROWSET in stored procedures

Posted on 2002-04-12
2
Medium Priority
?
624 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

604 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