tags:

views:

75

answers:

1
UPDATE employees
   SET job_id = (SELECT job_id
                   FROM employees
                  WHERE employee_id = 205),
       salary = (SELECT salary
                   FROM employees
                  WHERE employee_id = 205)
 WHERE employee_id = 114;

This is the query i have been using. Here i use 2 subqueries but they have the same where condition.. The seek time is doubled.. Is there a way to optimize the whole query to a single subquery?

Thanks in advance

+4  A: 

Hi Joseph,

you can remove a subquery if you update a set of columns:

UPDATE employees
   SET (job_id, salary) 
        = (SELECT job_id, salary FROM employees WHERE employee_id = 205)
 WHERE employee_id = 114;
Vincent Malgrat
+1 - I didn't know you could do SET (job_id, salary) = SELECT (Job_id, Salary). Thank you.
ydobonmai
@Vincent, Is that syntax applicable for SQL server as well?
ydobonmai
No, it does not work in sql server (2005)
igor