Solved

Issue with using OpenQuery that contains variables

Posted on 2011-02-20
8
338 Views
Last Modified: 2012-05-11
Hi

Reference to my previous question at: http://www.experts-exchange.com/Programming/Languages/SQL_Syntax/Q_26819024.html

I executed the following code:

select c.* into #temp from OpenQuery(MY_SERVER_NAME,'exec MY_DB_NAME.dbo.MY_PROCEDURE_NAME ''VAL1'',''VAL2'',''VAL3'',''VAL4''') as c
select * from #temp
drop table #temp

Open in new window


I get the expected result.

However, I want now to pass to that procedure a variable instead of static value (as those VAL1,VAL2 ... etc)

So I did it this way:

CREATE PROCEDURE [dbo].[MY_NEW_PROCEDURE_NAME](@VAR1 VARCHAR(15), @VAR2 VARCHAR(2), @VAR3 INT)
AS
BEGIN
	select c.* into #temp from OpenQuery(MY_SERVER_NAME,'exec MY_DB_NAME.dbo.MY_CURRENT_PROCEDURE_NAME '''+@VAR1+''', '''+@VAR2+''', ''SOME_VALUE1'+@VAR3+''', ''SOME_VALUE2'+@VAR3+'''') as c
	select * from #temp
	drop table #temp
END

Open in new window


It gives me the following error message when I try to create this new procedure:

Msg 102, Level 15, State 1, Procedure MOVEMENT_HISTORY_REPORT_YEARLY, Line 10
Incorrect syntax near '+'.

So what's the problem here?
0
Comment
Question by:alfardan
  • 3
  • 3
  • 2
8 Comments
 
LVL 26

Expert Comment

by:tigin44
Comment Utility
try this
CREATE PROCEDURE [dbo].[MY_NEW_PROCEDURE_NAME](@VAR1 VARCHAR(15), @VAR2 VARCHAR(2), @VAR3 INT)
AS
BEGIN
	select c.* into #temp from OpenQuery(MY_SERVER_NAME,'exec MY_DB_NAME.dbo.MY_CURRENT_PROCEDURE_NAME @VAR, @VAR2, SOME_VALUE1+@VAR3, SOME_VALUE2+@VAR3) as c
	select * from #temp
	drop table #temp
END

Open in new window

0
 
LVL 26

Assisted Solution

by:tigin44
tigin44 earned 100 total points
Comment Utility
Here I dont have an sql interface... sory I missed a point in the above post..
 try this

CREATE PROCEDURE [dbo].[MY_NEW_PROCEDURE_NAME](@VAR1 VARCHAR(15), @VAR2 VARCHAR(2), @VAR3 INT)
AS
BEGIN
	select c.* into #temp from OpenQuery(MY_SERVER_NAME,'exec MY_DB_NAME.dbo.MY_CURRENT_PROCEDURE_NAME '+ @VAR+', '+ @VAR2 + ', ' + CAST(SOME_VALUE1+@VAR3 AS varchar(20)) +', '+ CAST(SOME_VALUE1+@VAR3 AS varchar(20))+') as c
	select * from #temp
	drop table #temp
END

Open in new window

0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 400 total points
Comment Utility
you cannot use variables in openquery ...
you have to build a dynamic query ...
but then, the INTO #temp table will be a problem, you cannot do that, as the table would be dropped being in dynamic sql ...

declare @sql varchar(max)
set @sql = 'select c.* into #temp from OpenQuery(MY_SERVER_NAME,''exec MY_DB_NAME.dbo.MY_PROCEDURE_NAME ''''' + @VAL1 + ''''',''''' + @VAL2 + ''''',''''' + @VAL3 + ''''',''''' + @VAL4 + ''''' '') as c 
select * from #temp
drop table #temp ' 

Open in new window


note that if any of your variables could contain a quote itself, you have to escape the quotes (welcome to the quoting hell):

declare @sql varchar(max)
set @sql = 'select c.* into #temp from OpenQuery(MY_SERVER_NAME,''exec MY_DB_NAME.dbo.MY_PROCEDURE_NAME ''''' + replace(@VAL1 , '''', '''''''''' ) + ''''',''''' + @VAL2 + ''''',''''' + @VAL3 + ''''',''''' + @VAL4 + ''''' '') as c 
select * from #temp
drop table #temp ' 

Open in new window


hope this helps

0
 

Author Comment

by:alfardan
Comment Utility
tigin44:

Same error message, won't work

angelIII:

So you're saying I can not use a var in an openquery at all and there is no work-around for it? Your example already gives an issue as you stated because the tem table is going to be droped right away, which what I don't want.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 400 total points
Comment Utility
as I stated, you cannot use "#temp" table, but you could use ##temp table, or a normal table instead.
0
 

Author Comment

by:alfardan
Comment Utility
I tried both local and global temp tables, and built my query dynamically using .net code and passed it to SQl server, but also got into an issue there.

it looks like there is no way to pass vars to openquery function, which is a really annouying restriction in MS SQL.

Therefore, I'll just distribute credits on participants for their attempt to help, but not for the correct solution becasue there is no such a one.
0
 

Author Closing Comment

by:alfardan
Comment Utility
Reason of grade (B) is stated in my last comment
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
>but not for the correct solution becasue there is no such a one.
I can understand your frustration just too well, I have the same issue every day...
besides of that: "you can't do it" is a correct solution, according to EE grading rules.
=> you shall not shoot the pianist/messenger/etc for getting bad news ...
CHeers
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now