Solved

Issue with using OpenQuery that contains variables

Posted on 2011-02-20
8
341 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
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 100 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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 400 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 400 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 142

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help With Database JOIN 7 28
Select only the top record in a left join 13 35
Run SQL Server Proc from Access 11 31
Need help with a Stored Proc on Sql Server 2012 4 9
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

772 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