Hello All, when I write VBA, I can declare an array as a variable as varName(2) and then reference that variable in a loop with the variable varName plus a counter, something like:
dim varName(2) as variant
dim slqcmd as string
dim i as integer
do until i = 3
set i = 1
sqlcmd = "select * from " & varName(i) 'here is where the variable gets concatenated with the loop counter
set i = i+1
Can i do something similar in sql? Doing it in VBA requires declaring an array so that it recognizes the change in the variable (i.e. adding the "i" to varName to make it varName1, then after 1st loop, varName2 etc..
The basics of the sql I want are as below, so like the above VBA code above, I'm trying to concatenate the "Tbl" with @cnt to get the first table so the variable name would have to change to @tbl1, then @tbl2 etc..of course, this doesn't work since @tbl isn't a recognized variable - trying to know how to concatenate "Tbl" with @cnt in some way prior to the engine knowing its not a variable - i also tried a bit of dynamic sql - but that doesn't seem to work as it then views the variable as text..
Note, I do know how to create a table for this and then then cursor through the recordset, but want to know if this can be done this 'array' way way for conceptual reasons..(if, however, it can't be done the way I'm trying, i suppose i'll have to resort to creating the table..)
Thanks for any help..
set @Tbl1 = 'Dim_Proj'
set @Tbl2 = 'Dim_Meas'
set @cnt = 1
WHILE @cnt < 3
PRINT 'SELECT * FROM ' + @tbl + CAST(@cnt AS VARCHAR(10))
set @cnt = @cnt + 1