Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

UPDATE dbo.TestStudents  

SET LASTNAME = 

( CASE  

WHEN (LASTNAME = 'AAA') THEN 'BBB' 

WHEN (LASTNAME = 'CCC') THEN 'DDD' 

WHEN (LASTNAME = 'EEE') THEN 'FFF' 

ELSE  (LASTNAME)

END )

The statement work for the purpose but the else condition scan through every record in the table. Is there any way I can leave the unaffected rows as they are?

1 Answer

0 votes
by (40.7k points)

Try adding a WHERE clause like this:

UPDATE dbo.TestStudents  

SET     LASTNAME =  CASE  

                        WHEN LASTNAME = 'AAA' THEN 'BBB' 

                        WHEN LASTNAME = 'CCC' THEN 'DDD' 

                        WHEN LASTNAME = 'EEE' THEN 'FFF' 

                        ELSE LASTNAME

                    END 

WHERE   LASTNAME IN ('AAA', 'CCC', 'EEE')

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

30.5k questions

32.6k answers

500 comments

108k users

Browse Categories

...