update value in column and have a trigger update another column depending on the value

mHelpMe

I have a table. In this table there are three columns I am interested in creating a trigger on.

My table - tblABC

column name       data type

Id                int (primary key)
Live              bit
CompletionDate    date
CancelDate        date

So I would like the trigger to execute whenever the CompletionDate or CancelDate is updated.

I want the trigger to do one of two things

1) if either the CompletionDate or CancelDate columns date is changed to a date that is equal to today or less then I want the Live column to equal 0.

2) if either the CompletionDate or CancelDate columns date is changed to a date that is greater than today or NULL then I want the Live column to equal 1.

Below is my code. I am not sure if its correct also I'm not sure how this will deal with NULLS?

CREATE TRIGGER TR_MYTRIGGER
ON  tblABC
AFTER UPDATE

update d
set d.Live = 0
from tblABC e d 
inner join inserted i on d.Id = i.Id
where i.CompletionDate <= GETDATE() or i.CancelDate <= GETDATE()

update d
set d.Live = 1
from tblABC d 
inner join inserted i on d.Id = i.Id
where i.CompletionDate > GETDATE() and i.CancelDate > GETDATE()
Sam

As you said in your second point, if either of the two dates is set to greater than today or set to null then you need to set your live column to 1. Change your second update query to this.

update d
set d.Live = 1
from tblABC d 
inner join inserted i on d.Id = i.Id
where (i.CompletionDate > GETDATE() or i.CancelDate > GETDATE() 
or i.CompletionDate is null or i.CancelDate is null) and d.Live <> 1

But as suggested in the comments, a computed column seems like a better option.

Edit For performance optimization, you shouldn't update the rows that are already 1. See updated query. Do the same for your first update query.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

delete or update depending column value

From Dev

MySQL update column which is a value in another column

From Dev

MySQL update column which is a value in another column

From Dev

Update column from another table column value

From Dev

How to update a column using another column value?

From Dev

Using CASE to update column value depending on other column values

From Dev

Update column value by querying concatenated column values with another table value

From Dev

MySQL update column value with max value from another column

From Dev

UPDATE sql column with value from another column based on a date column

From Dev

How to update a column with another tables columns value?

From Dev

Update newly added column with value on another table

From Dev

mysql update column value from another table

From Dev

Update column value with trigger postgresql/ fetch value from insert query

From Dev

Column loop and update another column with COUNT() value from another table

From Dev

SQL selecting column depending on the value of another column

From Dev

Retrieve value from column depending on another column

From Dev

Column value incremental update

From Dev

Update a column with a calculated value

From Dev

update DataGridView column value

From Dev

Update column to a value in joined column

From Dev

Update value of other column on update of this column

From Dev

update a column with derived value from another column in mysql

From Dev

Update value in a column based on another column in the same table in MYSQL

From Dev

How to update a Pandas datafrae column based on another column value?

From Dev

How to update a column using another column's value with a condition?

From Dev

update a column with derived value from another column in mysql

From Dev

SQL Server - Select column to update based on another column value

From Dev

Update value in column based on column count in another table with mysql

From Dev

Fetch XML value from one column and update it in another column

Related Related

  1. 1

    delete or update depending column value

  2. 2

    MySQL update column which is a value in another column

  3. 3

    MySQL update column which is a value in another column

  4. 4

    Update column from another table column value

  5. 5

    How to update a column using another column value?

  6. 6

    Using CASE to update column value depending on other column values

  7. 7

    Update column value by querying concatenated column values with another table value

  8. 8

    MySQL update column value with max value from another column

  9. 9

    UPDATE sql column with value from another column based on a date column

  10. 10

    How to update a column with another tables columns value?

  11. 11

    Update newly added column with value on another table

  12. 12

    mysql update column value from another table

  13. 13

    Update column value with trigger postgresql/ fetch value from insert query

  14. 14

    Column loop and update another column with COUNT() value from another table

  15. 15

    SQL selecting column depending on the value of another column

  16. 16

    Retrieve value from column depending on another column

  17. 17

    Column value incremental update

  18. 18

    Update a column with a calculated value

  19. 19

    update DataGridView column value

  20. 20

    Update column to a value in joined column

  21. 21

    Update value of other column on update of this column

  22. 22

    update a column with derived value from another column in mysql

  23. 23

    Update value in a column based on another column in the same table in MYSQL

  24. 24

    How to update a Pandas datafrae column based on another column value?

  25. 25

    How to update a column using another column's value with a condition?

  26. 26

    update a column with derived value from another column in mysql

  27. 27

    SQL Server - Select column to update based on another column value

  28. 28

    Update value in column based on column count in another table with mysql

  29. 29

    Fetch XML value from one column and update it in another column

HotTag

Archive