Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Update single row according to multiple criteria with a single update


Update single row according to multiple criteria with a single update

Author
Message
rroque
rroque
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 78
If you have to give a flat hike to your employees using the following criteria:

Salary between 30000 and 40000 = 5000 hike
Salary between 40000 and 55000 = 7000 hike
Salary between 55000 and 65000 = 9000 hike

In this situation many developers tend to use a cursor, determine each employee's salary and update his salary according to the above formula. But the same can be achieved by multiple update statements or can be combined in a single UPDATE statement.

Please give an example of a single UPDATE statement below:
Matt Miller (#4)
Matt Miller (#4)
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8349 Visits: 18275
Hmm...sounds a bit like homework.

You probably want to take a look at the CASE statement, and use it appropriately in an UPDATE statement.

----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
rroque
rroque
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 78
Thanks guys. Heres the answer. I Completely forgot about the case clause.

UPDATE Salaries
SET Salary =
CASE
WHEN (Salary between 30000 and 40000) THEN Salary + 50000
WHEN (Salary between 40000 and 55000) THEN Salary + 70000
WHEN (Salary between 55000 and 65000) THEN Salary + 90000
END
PW-201837
PW-201837
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1457 Visits: 1228
>>WHEN (Salary between 30000 and 40000) THEN Salary + 50000

Wow, that's a greater than 100% salary hike. Where is this place, I want to apply for a job Wink
Matt Miller (#4)
Matt Miller (#4)
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8349 Visits: 18275
PW (5/22/2008)
>>WHEN (Salary between 30000 and 40000) THEN Salary + 50000

Wow, that's a greater than 100% salary hike. Where is this place, I want to apply for a job Wink


oops. hehe...that's what happens when you start coding on less than a 1/4 cup of coffee. Just kept on adding zeroes on there.....

----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
John Rowan
John Rowan
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4392 Visits: 4529
In this situation many developers tend to use a cursor


Developers resort to using cursors for 2 reasons:
1. The problem domain's complexity makes it difficult for them to envision the set based answer.

2. They do not understand set based thinking.

In this example, the only reason anyone would resort to a cursor would be becuase of #2. This is a very basic update statement and considering a cursor to solve this means that there may be a lack of understanding of working with sets of data.

Rroque,

This is not a slam on you in any way so please don't take this as such, but this is the second post I've seen from you today where cursors were the first choice. As time allows, search through SSC for "set based" and read through the relevent results. Once you condition yourself to see data as sets and work with them as such, using cursors and loops will not even enter into your normal thought process. If you have the desire to be a notch above the masses, think set based. Smile

John Rowan

======================================================
======================================================
Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
rroque
rroque
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 78
As you once did, right JOHNROWAN!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search