[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Variable declaration in EXEC

Posted on 2001-06-22
10
Medium Priority
?
638 Views
Last Modified: 2008-02-26
Hi,

    I'm having a strange problem with a query that I hope someone may have seen before.  I've declared a variable @statvar as type int and @cmd1 as a varchar (255).

The statement where I'm having the problem is as follows:

set @cmd1='select @statvar=count(*) from '+@dbname+'..sysfiles'

exec(@cmd1)

I keep getting the error "Must declare the variable @statvar"

I've worked around the problem by eliminating the '@statvar= ' and sending the results to a temp table instead, but I'm curious as to why the original query doesn't work.  I don't have any 'go' statements prior to it.

Thanks in advance!
0
Comment
Question by:DecaysChampion
[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
  • 2
  • 2
  • 2
  • +3
10 Comments
 
LVL 30

Accepted Solution

by:
SteveGTR earned 200 total points
ID: 6219361
Hi,

The @statvar is out of scope when you do exec. I've found success programming dynamic SQL statements using cursors:

exec("declare hC1 cursor GLOBAL READ_ONLY for " +
  "select count(*) from " + @dbname + "..sysfiles")

open hC1
fetch hC1 into @statvar
close hC1
deallocate hC1

Good Luck,
Steve
0
 
LVL 3

Expert Comment

by:mathavra
ID: 6219397
Try "select" instead of "set" as below:

select @cmd1='select @statvar=count(*)from '+@dbname+'..sysfiles'

Or

Is it OK for you move the declare statments just before the SQL statment as below:

declare @statvar int
select @cmd1='select @statvar=count(*)from '+@dbname+'..sysfiles'
0
 

Author Comment

by:DecaysChampion
ID: 6219431
Mathavra,

The problem occurs regardless of where the DECLARE statement is and I've verified that the actual command is being correctly stored in @cmd1 using SET (It occurs during the exec statement).
0
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 3

Expert Comment

by:mgmanoj
ID: 6219441
When you assign value to a variable inside the dynamic sql that variable is treated as local variable to that sql statement. When you execute a dynamic sql it treats that sql statement as new bunch of statement (what i understood might be it must be executing with new connection)
0
 

Expert Comment

by:CharlesM
ID: 6219479
mq is correct, it's seen as a local variable, ergo, this will work fine as @statvar is declared within the string:

set @cmd1='declare @statvar int; select @statvar=count(*)from '+@dbname+'..sysfiles; select @statvar;'
exec(@cmd1)
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 6219503
You are correct Charles, but after the exec returns the @statvar will be unaccessible (out of scope).

My cursor example get's around this as does DecaysChampion's temp table solution.
0
 

Expert Comment

by:CharlesM
ID: 6219510
Proof of theory ;o)
0
 
LVL 3

Expert Comment

by:mathavra
ID: 6219748
Whatever I proposed did not work for me also. I just tried. Sorry for the incorrect answer.

I think, each EXEC statement is executed as a (separate) batch and that is reason why it is not recognizing the variable declaration. Also, it cannot return any variable since it is out of scope as mqmanoj.

May be you want to use stored procedure and return the appropriate value from the stored procedure.
0
 

Author Comment

by:DecaysChampion
ID: 6219827
Thank you all for your comments!
0
 
LVL 18

Expert Comment

by:nigelrivett
ID: 6220519
Sorry to be late but you can set return parameters from dynamic sql and dynamic sql SP calls if you use sp_executesql instead of exec.

You cannot set parameters from an exec but you can from sp_executesql.
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

649 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