is it possible to do dynamic declarations?
I will explain: I have a table COLUMNAMES:
ID|Name
1|Country
2|City
3|District
4|Neighbourhood
For each record in that table I would like to do something like:
declare @i int = 1
declare @number int
set @number = (SELECT count(*) FROM COLUMNNAMES)
While @i <= @number
BEGIN
Execute ('Declare column' + @i +'varchar(25)')
Execute ('set column' + @i +' = (Select NAME from COLUMNAMES where id = ' + @i)
set @i = @i + 1
END
The idea is that I get a list of variables (strings) that I can use to create SELECT statements with dynamic table-aliases:
Execute ('Select SOMECOLUMN as ' + @columname + @i +', ANOTHERCOLUMN as ' + @columname + @i +', ATHIRDCOLUMN as ' + @columname + @i + ' FROM SOMETABLE')
Can this be done? If so, how?
This is not complete solution but a direction . you need to get the value ‘SomeColumn’ dynamically someway ,likely the way you are getting the aliases in the below solution.