Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ID   NAME              SAL    DEP   LOC
----  ------ ---------- ---------- ----- -----
  1   viki              1000   it    cb
  2   allen             2000   ac    ch
  3   smith             2000   ac    sa
  4   adam              5000   it    ch
  5   will              4000   ac    cb
  6   john              6000   it    cb
  7   bradley          10000   it    ch


i have table like this if dep = 'it' update 1000 if dep='ac' update 2000


I tried this query but it fails
SQL
update emp set sal=sal+(select case when dep='it' then '1000'
                                    when dep='ac' then '2000'
                                    else '0'
                                    end
                                    from emp);
Posted
Updated 7-Mar-14 7:20am
v2

Try something like this:
SQL
UPDATE t1 SET t1.sal = t2.sal
FROM emp AS t1 INNER JOIN (
    SELECT id, CASE WHEN dep='it' THEN sal+1000
                   WHEN dep='ac' THEN sal+2000
                   ELSE sal
               END AS sal
    FROM emp
    ) AS t2 ON t1.id = t2.id
 
Share this answer
 
v2
Comments
j viky 7-Mar-14 13:32pm    
with out using case is there any option sir,
please guide me if it is possible
Maciej Los 7-Mar-14 13:34pm    
In a single statement? No!
j viky 7-Mar-14 13:44pm    
ok sir thanx spending your valuable time by clearing my doubts
Peter Leow 7-Mar-14 13:47pm    
Hi, Maciej, your solution works too, +5!
Maciej Los 7-Mar-14 13:48pm    
Thank you, Peter.
BTW: but where is a 5?
SQL
update emp set sal=sal+
(
  case when dep='it' then 1000
       when dep='ac' then 2000
       else 0
       end
)
 
Share this answer
 
Comments
j viky 7-Mar-14 13:39pm    
perfectly works thank you so much sir@peter
Maciej Los 7-Mar-14 14:06pm    
Simplest solution than my...
+5!
SQL
update emp e1 set e1.sal=e1.sal + (select case when e1.dep='it' then '1000'
                                    when e1.dep='ac' then '2000'
                                    else '0'
                                    end)
 
Share this answer
 
Comments
j viky 7-Mar-14 13:37pm    
IT says unexpected end of sql command sir

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900