Skip to content
Advertisement

Update same table as selected with calculated values using resultset

I’m new to sql (scripting). Single CRUD commands are no problem, but I’m trying to create a stored procedure that wil update a table with a calculated value using the results from the same selected table.

In the table there is a value with the total vacation-hours for each employee and a value of the remainder of the vaction-hours of the last year.

What I will try to achieve is to ADD the new hours for this year to the remaining hours. There is one catch. The new hours column is in whole hours, the remaining-hours is a value in ‘ticks’ which is hundred nanoseconds.

How can I update the table using the same table as a reference for this calculation?

example table:
employee, vacation, remainingvacation

new value of remainigvacation = vacation(in hours) * 60(minutes) * 60(seconds) * 10^9 (to convert to ticks) + remainingvacation (in ticks)

Advertisement

Answer

UPDATE
  myTable
SET
  remainingvacation = (vacation * 60 * 60 * POWER(10, 9)) + remainingvacation

You can then do the same after including JOINS and WHERE clauses…

UPDATE
  myTable
SET
  remainingvacation = (vacation * 60 * 60 * POWER(10, 9)) + remainingvacation
FROM
  myTable
INNER JOIN
  yourTable
    ON myTable.id = yourTable.id
WHERE
  yourTable.value = 'Egyptian Holidays'
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement