Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 Update single row according to multiple criteria with a single update Rate Topic Display Mode Topic Options
Author
 Message
 Posted Thursday, May 22, 2008 9:01 AM
 Grasshopper Group: General Forum Members Last Login: Saturday, April 06, 2013 10:09 AM 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 hikeSalary between 40000 and 55000 = 7000 hikeSalary between 55000 and 65000 = 9000 hikeIn 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:
Post #505254
 Posted Thursday, May 22, 2008 9:19 AM
 SSCertifiable Group: General Forum Members Last Login: Yesterday @ 9:41 PM Points: 7,090, Visits: 14,715
 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?
Post #505262
 Posted Thursday, May 22, 2008 10:39 AM
 Grasshopper Group: General Forum Members Last Login: Saturday, April 06, 2013 10:09 AM Points: 11, Visits: 78
 Thanks guys. Heres the answer. I Completely forgot about the case clause.UPDATE SalariesSET 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 + 90000END
Post #505320
 Posted Thursday, May 22, 2008 11:34 AM
 Ten Centuries Group: General Forum Members Last Login: Saturday, March 08, 2014 4:25 PM Points: 1,415, Visits: 1,228
 >>WHEN (Salary between 30000 and 40000) THEN Salary + 50000Wow, that's a greater than 100% salary hike. Where is this place, I want to apply for a job ;)
Post #505355
 Posted Thursday, May 22, 2008 11:48 AM
 SSCertifiable Group: General Forum Members Last Login: Yesterday @ 9:41 PM Points: 7,090, Visits: 14,715
 PW (5/22/2008)>>WHEN (Salary between 30000 and 40000) THEN Salary + 50000Wow, that's a greater than 100% salary hike. Where is this place, I want to apply for a job ;)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?
Post #505368
 Posted Thursday, May 22, 2008 1:28 PM
 Hall of Fame Group: General Forum Members Last Login: Wednesday, October 23, 2013 12:46 PM Points: 3,843, Visits: 3,833
 In this situation many developers tend to use a cursorDevelopers 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. :) John Rowan ============================================================================================================ Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
Post #505446
 Posted Thursday, May 22, 2008 3:06 PM
 Grasshopper Group: General Forum Members Last Login: Saturday, April 06, 2013 10:09 AM Points: 11, Visits: 78
 As you once did, right JOHNROWAN!
Post #505504

 Permissions