• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 357
  • Last Modified:

Issue with using OpenQuery that contains variables

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
alfardan
Asked:
alfardan
  • 3
  • 3
  • 2
3 Solutions
 
tigin44Commented:
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
 
tigin44Commented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 
alfardanAuthor Commented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
as I stated, you cannot use "#temp" table, but you could use ##temp table, or a normal table instead.
0
 
alfardanAuthor Commented:
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
 
alfardanAuthor Commented:
Reason of grade (B) is stated in my last comment
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now