0

For example, I query the database,

SELECT SALARY, WEEKLY_HOURS FROM EMPLOYEE WHERE EMP_ID = 999

Then I compare SALARY and WEEKLY_HOURS to local variables. If my local values are different, I ran an update query:

UPDATE EMPLOYEE SET SALARY = 255, WEEKLY_HOURS = 72 WHERE EMP_ID = 999

And if the values are no difference, I skip the update query.

So my question is, am I able to do the comparison and update in one query without stored procedure? Just trying to save one round trip.

Thanks.

Cal
  • 747
  • 1
  • 13
  • 30
  • possible duplicate of [Is there a way to SELECT and UPDATE rows at the same time?](http://stackoverflow.com/questions/497464/is-there-a-way-to-select-and-update-rows-at-the-same-time) – Scott Solmer Oct 16 '13 at 22:02

1 Answers1

1
update e
set
 e.Salary = 255
 , e.Weekly_Hours = 72
from
 Employee e
where
 e.Emp_ID = 999
 and (
   e.Salary <> 255 or
   e.Weekly_Hours <> 72 )
Moho
  • 15,457
  • 1
  • 30
  • 31