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

x
?
Solved

Issue with using OpenQuery that contains variables

Posted on 2011-02-20
8
Medium Priority
?
355 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
[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
  • 3
  • 3
  • 2
8 Comments
 
LVL 26

Expert Comment

by:tigin44
ID: 34936661
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 300 total points
ID: 34936673
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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1200 total points
ID: 34937122
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:alfardan
ID: 34940114
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
 
LVL 143

Assisted Solution

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

Author Comment

by:alfardan
ID: 35077471
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
ID: 35077479
Reason of grade (B) is stated in my last comment
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35080091
>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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

618 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