Execute @SQL That contains 'Select @Variable = Field1 from Table'


Hello

I'm trying to execute some dynamic sql however I'm running into a problem with my variables .

code sample

set @SQL = 'Select @Variable = Field1 from ' + @DBNAME + '.dbo.Table'
exec @SQL

error Msg 137 ... Must declare the variable @Variable.

Since the @variable is to be used to store the returned field value I can't simply do  'Select ' + @Variable + ' = Field1 from ' + @DBNAME + '.dbo.Table' or else it evaluates the @variable to nothing and the query ends up being ...  'Select  = Field1 from DbName.dbo.Table' which of course fails.

How can I format this to work.

DarebearAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wnrossCommented:
DECLARE @Variable varchar(120)
set @SQL = 'Select @Variable = Field1 from ' + @DBNAME + '.dbo.Table'
exec @SQL
GO

Try that.

Cheers,
-Bill
DarebearAuthor Commented:
Bill

Sorry the variable is actually declared further up in my code not shown..I gues my example is a bit misleading. Your code sample will fail.  Apparently you can't use exec with dynamic sql that returns data into a variable. I'm looking for another way to do it with out using a temp table. I have about 50 of these statements in my code. I'd be curious to know if there are any sql 2005 workarounds?
wnrossCommented:
Sorry, not that I'm aware of, since the variables are scoped by the execute statement itself:

exec sp_executesql @SQL

is actually

exec (sp_executesql @SQL)

So no outer variables can be affected by an execute statement.  
Temptables are your best option here:
create table ##c (
       temp varchar(100)
)
set @sql = 'insert into ##c Select Field1 from ' + @DBNAME + '.dbo.Table'
exec (@sql)
select @Variable = temp from ##c

-- Display Results
select @Variable

drop table ##c

Cheers
-Bill

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wnrossCommented:
Thanks for the points, did that get you past your hurdle?

-Bill
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.