Solved

Using parameterized SQL statements with OPENROWSET in stored procedures

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

Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

Question has a verified solution.

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

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
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…
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…

691 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