August 30, 2012 at 1:02 pm
Why does not the four-part naming convention work?!?
I need to pass a parameter and am struggling to make this work in OpenQuery:
@STUD_ID is varchar(8) and is the incoming parameter to a stored procedure which places an openquery call, passing along the parameter once again:
select <stuff ...>
from OPENQUERY(Banner_Prod, 'SELECT a.Major
,a.FIRSTNAME AS NameFirst
,a.MIDDLENAME AS NameMiddle
,a.LASTNAME AS NameLast
,a.CLASSCODE AS PersonSubType
FROM fredtrailer.student_Directory_info a
WHERE a.STUD_ID = ''''' + @STUD_ID + '''''')
How to do this? I've played with the number of single quotes. I also put the sql query into a variable and tried to put the @sqlquery variable in place of the text, but it wants text there, apparently.
Thanks for any help!
August 30, 2012 at 1:53 pm
You could resort to dynamic sql, double every single quote.
something like
set @mysql='select .... from openquery(banner_prod,''SELECT a.Major
,a.FIRSTNAME AS NameFirst
,a.MIDDLENAME AS NameMiddle
,a.LASTNAME AS NameLast
,a.CLASSCODE AS PersonSubType
FROM fredtrailer.student_Directory_info a
WHERE a.STUD_ID ='''+@STUD_ID+''')'
execute @mysql
August 30, 2012 at 2:14 pm
Thanks, Joe, once again.
I just came back to report that I had resolved it in exactly that way. And you beat me to it.
August 30, 2012 at 8:52 pm
It's been several years since I've had to work with OPENQUERY but I believe the following will work...
select <stuff ...>
from OPENQUERY(Banner_Prod, 'SELECT a.Major
,a.FIRSTNAME AS NameFirst
,a.MIDDLENAME AS NameMiddle
,a.LASTNAME AS NameLast
,a.CLASSCODE AS PersonSubType
FROM fredtrailer.student_Directory_info a'
) oq
WHERE oq.STUD_ID = @STUD_ID
The only thing that I don't remember is whether or not the external criteria is reflected in the OPENQUERY or if the whole OPENQUERY result set is returned before the criteria is applied. The performance test for that is, of course, simple enough. Give it a try.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 31, 2012 at 6:26 am
Jeff,
Thanks for this. I must say that I'm a bit surprised that it is really usable. It looks like it would pull the entire table over from Oracle to SQL Server and then select a single record.
But it is just as performant as the other solution presented here (dynamic SQL) as perceived from the SQL side. I think I'll ask the Oracle DBA to watch on the Oracle side as I query each way to see what kind of difference there may be on that end.
I'll post back with results.
Mike
August 31, 2012 at 6:48 am
Explain plan and resource consumption reports on the Oracle side actually slightly favor the non-dynamic SQL version, although the difference between the two is negligible.
That's not serious benchmarking, just running each example a couple of times.
Good enough, though, for what I was after.
August 31, 2012 at 8:04 am
Michael Gerholdt (8/31/2012)
Explain plan and resource consumption reports on the Oracle side actually slightly favor the non-dynamic SQL version, although the difference between the two is negligible.That's not serious benchmarking, just running each example a couple of times.
Good enough, though, for what I was after.
Excellent! Thank you for checking on the Oracle side. I couldn't remember which way it worked and I really appreciate the feedback.
Shifting gears, this is a pretty good indication that an index (if not already present) on the Oracle side would help, as well.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 2, 2012 at 9:23 am
Stumbled across this Q&A thread using Google. Jeff's solution was ideal for my situation. Thank you!
December 11, 2013 at 6:59 am
Jeff Moden (8/30/2012)
It's been several years since I've had to work with OPENQUERY but I believe the following will work...
select <stuff ...>
from OPENQUERY(Banner_Prod, 'SELECT a.Major
,a.FIRSTNAME AS NameFirst
,a.MIDDLENAME AS NameMiddle
,a.LASTNAME AS NameLast
,a.CLASSCODE AS PersonSubType
FROM fredtrailer.student_Directory_info a'
) oq
WHERE oq.STUD_ID = @STUD_ID
The only thing that I don't remember is whether or not the external criteria is reflected in the OPENQUERY or if the whole OPENQUERY result set is returned before the criteria is applied. The performance test for that is, of course, simple enough. Give it a try.
Thank you Jeff. This is great to read. I have searched but every solution comes with dynamic sql and I wanted to avoid it 🙂
Can we pass Linked Server Name as variable without dynamic sql ?
December 11, 2013 at 8:32 am
What I have posted isn't Dynamic SQL... it's a hardcoded query like you might have in any stored procedure but it's necessarily stored in a string simply because that's what OPENQUERY requires. I know that OPENROWSET doesn't allow the use of variables and must be dynamic to make changes but I haven't actually tried using a variable with OPENQUERY so I don't know if "simple" query replacement would work here or not. Of course, "full" dynamic SQL like that used for making changes to an OPENROWSET would work but I absolutely understand the desire to avoid such a thing.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply