TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Abraham Varghese
NA
5
0
Dynamically update the column name in SQL
Oct 21 2015 5:22 PM
How dynamically update the column name in SQL inside the case statement.
DECLARE @CNT INT=1
DECLARE @COL VARCHAR(15)='RM_TGT' + CONVERT(VARCHAR,@CNT)
DECLARE @Heading VARCHAR(25)=NULL
WHILE @CNT<=12
BEGIN
IF (@CNT=1 )
SET @Heading='TARGETS(S): '
ELSE
SET @Heading=''
UPDATE [DenaliConversion].[dbo].[RM_CAFILE_MO_FACTORS_NEW]
SET CUSTOM_TEXT_01 =CUSTOM_TEXT_01 + CHAR(10) + @Heading +
CASE
WHEN @COL='01' THEN 'ANIMALS/LIVESTOCK' + ', '
WHEN @COL='02' THEN 'AUTO PARTS' + ', '
WHEN @COL='03' THEN 'CASH/NOTES' + ', '
WHEN @COL='04' THEN 'CLASSROOM EQUIPMENT' + ', '
WHEN @COL='05' THEN 'CLOTHING/FURS' + ', '
ELSE
@COL + ', '
END
SET @CNT=@CNT + 1
END;
Reply
Answers (
1
)
Update Sql Column
Drop Table when it references by Other table