Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parameters in OPENROWSET?

Status
Not open for further replies.

spacedeveloper

Programmer
Aug 3, 2010
27
US
Hi everyone,

I must be missing something simple here, but I can't seem to find it. When I try to run this openrowset query I get an

"Incorrect syntax near '+'."

error near the line mentioned. Can someone help me find the problem? Thanks:

Code:
declare @PropID varchar(16);
declare @FolioNum int;
set @PropID = 'PID';
set @FolioNum = 17516;

SELECT 
	a.room
	, a.transactionnumber
	, LTRIM(RTRIM(CONVERT(VARCHAR(10),a.transactiondate,101))) AS transactiondate
	, a.accountnumber
	, ISNULL(a.accountdesc, '') AS accountdesc
	, CAST(a.credit AS decimal(18, 2)) AS Charges
	, CAST(a.debit AS decimal(18, 2)) AS Payments
FROM OPENROWSET
('SQLOLEDB'
, '' + @PropID + ''  --<-------*** ERROR MENTIONS THIS LINE *****
 ; 'userid' ; 
'password'
, 'select * from NV.dbo.Database WITH(NOLOCK) WHERE folionumber = ' + cast(@FolioNum as varchar(10)) + ' ORDER BY transactionnumber, transactiondate')
as a

Thanks for any help,

Frank
 
Hi Frank,

I think the problem is that the OPENROWSET doesn't like the use of variables. It would probably work if you had the literal value 'PID' rather than '' + @PropID + ''.

One potenital work around would be to prepare the command in a variable and execute it as dynamic SQL. You may be reluctant to go down that path - and that would be completely understandable.

Regards,
Tom
 
Thanks, I'll take a look at these suggestions and try to test them out.

The way I've been attempting to hit the problem is by dynamic sql...as much as I'd like to avoid it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top