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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

creating dymanic variables

Status
Not open for further replies.

camy123

Programmer
Mar 5, 2004
171
0
0
GB
does any one know how to create dynamic variables
 
i would like to create variables on the fly for example

@variable = 'masrket' + i


-- i = index value of loop
 
You can't really do that. Can you explain what you're trying to do (maybe post some code) and perhaps someone can suggest an alternative method?

--James
 
ok basically what i would like is to check 32 variables thats ive already declared.
I would like to loop through this variables checking there values
rather tyhen writing if statements for each variables
the variables are called market1..........market32

so my aim was to create a loop
example

while i< 32
begin
declare @variable varchar(2000)
set @variable = 'market'+ i
if @variable = 'example'
blah blah blah
""
""
end

the best way i reckon would be like this

while i< 32
begin
declare @variable varchar(2000)
declare @sql varchar(2000)

set @variable = 'market'+ i
set @sql = 'declare x varchar(2000)'
+ ' c = ' + @variable



blah blah blah
""
""
end
exec(@sql)

but it always asks for @market1 to be declared i cant declare it again as i will lose it value

hope this make sense
 
Rather than declare 32 variables, why don't you try creating a temp table with an identity column and inserting the 32 values into that. You could then loop through that table, selecting each value in turn into a single holder variable and do whatever you need to.

--James
 
yeh i know but was tryna keep it dynamic with less code...
basically tryan be to cleaver for me own good
thanks ne hows mate rerally appriciate your time..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top