Home Update a declared tabled dynamic column name
Reply: 0

Update a declared tabled dynamic column name

user1250
1#
user1250 Published in July 21, 2018, 9:47 am

I am running a SQL loop to update a declared table. Please have a look at the following

DECLARE @tblResults TABLE (  [idx]              INT IDENTITY(0,1)
                            ,[cityCode]         VARCHAR(3)
                            ,[month_1_perc]     INT
                            ,[month_2_perc]     INT
                            ,[month_3_perc]     INT
                            ,[month_4_perc]     INT
                            ,[month_5_perc]     INT
                            ,[month_6_perc]     INT
                            ,[month_7_perc]     INT
                            ,[month_8_perc]     INT
                            ,[month_9_perc]     INT
                            ,[month_10_perc]    INT
                            ,[month_11_perc]    INT
                            ,[month_12_perc]    INT)

loop will start here

DECLARE @colName VARCHAR(15)
DECLARE @sqlExec VARCHAR(500) = CONACT('UPDATE @tblResults SET [month_', @colName, '_perc] = 9; 

EXECUTE(@sql)

loop

This obviously doesn't work - I am trying to find a work around if any - Open to suggestions

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.307017 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO