Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Update multiple fields with different data Expand / Collapse
Author
Message
Posted Wednesday, January 09, 2013 7:44 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, February 15, 2013 9:50 PM
Points: 1, Visits: 2
Hi All,

I was just wondering if there was a way to update a field based on it's value multiple times but in one script without having to do a separate update statement every time as below. So for example:

UPDATE Table
SET col1 = 'aaa'
WHERE (department = 'AAA')

UPDATE Table
SET col1 = 'bbb'
WHERE (department = 'BBB')

UPDATE Table
SET col1 = 'ccc'
WHERE (department = 'CCC')

Thanks for your help in advance
Post #1405133
Posted Wednesday, January 09, 2013 9:03 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 8:37 PM
Points: 2,370, Visits: 3,252
You could possibly do this:

UPDATE    Table
SET col1 =CASE department
WHEN 'AAA' THEN 'aaa'
WHEN 'BBB' THEN 'bbb'
WHEN 'CCC' THEN 'cc'
ELSE col1 END
WHERE department IN ('AAA', 'BBB', 'CCC')





No loops! No CURSORs! No RBAR! Hoo-uh!

INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?

Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Are you too recursively challenged?
Splitting strings based on patterns can be fast!
Post #1405151
Posted Wednesday, January 09, 2013 10:46 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Sunday, February 17, 2013 5:40 PM
Points: 153, Visits: 172
Thanks heaps


Thanks,

Kris
Post #1405180
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse