Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to dynamically generate a variable name? 1

Status
Not open for further replies.

lalapaul

Programmer
Jul 27, 2004
2
US
Hi,

I need to dynamically generate a set of variable names. The name of these variable are the values generated by a query. Thanks.
 
I might need a little more information based on what you'll be selecting into it... but I think this is what you're looking for.

declare @var_gen varchar(1000)
declare @sql varchar(5000)

set @var_gen = (select 'variable')-- from your_query)

set @sql = ('
declare @' + @var_gen + ' varchar(100)

set @variable = ''test''
print @variable
')

print @sql
exec(@sql)

Does that make sense? If not, post more about your query and how you'll be deciding on the variable names and I'll try to help more.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top