In a table i had field called Status and it is of type integer and another two field From and To ... they are of type date .....
what i want to do is to change the value of status field when the current date become equal to the From field and also make another change when the current date become equal to the To field ...
So How can i do This on SQL server 2005?
You need to run a query to update the rows, something like this
UPDATE mytable
SET Status = CASE
WHEN datediff(d, From, getdate()) = 0 THEN 1
WHEN datediff(d, To, getdate()) = 0 THEN 2
END
You could create a calculated column based on the same expression if you want status to always be up to date without running the query first. Otherwise schedule a job to run that query every night so that the status column is always correct.
|||
Thank You for your reply and it is helpful,
But what if the From and To fields in a child table to the original table that have the field status?
e.x. Master Table have the following fields:
PersonID, Name, Status
Details Table has the following fields:
ID,PersonID (as foreign key to the master table), From, To
How the query will look like?!
|||
UPDATE mastertable
SET Status = CASE
WHEN datediff(d, detailtable.From, getdate()) = 0 THEN 1
WHEN datediff(d, detailtable.To, getdate()) = 0 THEN 2
END
FROM mastertable
INNER JOIN detailtable ON mastertable.PersonID = detailtable.PersonID