I have multiple columns with names like "abc", "abc1", "abc2", "abc3". My goal is to push the value to the next column, so:
abc3 = abc2
abc2 = abc1
abc1 = abc
abc = null
I wrote this:
DECLARE @name varchar(20)
SET @name = 'abc'
DECLARE @exec nvarchar(max)
SET @exec =
'UPDATE ' + @tbl + '
SET ' + @name + '3 = ' + @name + '2,
' + @name + '2 = ' + @name + '1,
' + @name + '1 = ' + @name + ',
' + @name + '= ''''
EXEC sp_executesql @exec
But if I want to change the name "abc" in something else and execute the query once again, the name does not update.
I found a way to assign the value within the sp_executesql command, however I'm not able to concat the namevalue with the numbers that way.
Is it possible to create a query like that, where I can execute it multiple times with different names?
My idea is to run it like that and change the names there.
EXEC sp_executesql @exec, N'@name nvarchar(20)', @name = N'abc'
EXEC sp_executesql @exec, N'@name nvarchar(20)', @name = N'xyz'