While I was working on a small task I came across a situation
DECLARE @i INT ,
@l INT ,
@desc CHAR(50)
SET @l = 1
SET @i = 20
WHILE ( @l <= @i )
BEGIN
SELECT 'Test' AS 'Test'+@l
SET @l = @l + 1
END
In this code I want when a loop run all time column name changed with the value of @l.
You need to build the statement dynamically and use EXECUTE or sp_executesql to execute the statement.
Recomended reading when entering the world of dynamic SQL :The Curse and Blessings of Dynamic SQL
Using sp_executesql instead: