May 22, 2008 at 9:01 am
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:
May 22, 2008 at 9:19 am
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?
May 22, 2008 at 10:39 am
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
May 22, 2008 at 11:34 am
>>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 😉
May 22, 2008 at 11:48 am
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?
May 22, 2008 at 1:28 pm
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. 🙂
May 22, 2008 at 3:06 pm
As you once did, right JOHNROWAN!
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply