Solved

Issue with using OpenQuery that contains variables

Posted on 2011-02-20
8
342 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 143

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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

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 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 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can i use WITH CTE for checking exist value? 3 47
Help Required 2 39
sql server concatenate fields 10 38
Present Absent from working date rage 11 38
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 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