Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Administration (2000)
 Change status

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2003-09-02 : 06:55:17
JOE writes "I have a status column and an expiredDate column. When the expired date exceeds the current date, I want to change the status field to "Expired". The only way I can think to do this is via some scheduled event or something. Any other ideas?"

X002548
Not Just a Number

15586 Posts

Posted - 2003-09-02 : 12:00:15
ummm....use a trigger?

something like...


USE Northwind
GO

CREATE TABLE myTable99 (col1 int IDENTITY(1,1), col2 varchar(10), col3 datetime
, PRIMARY KEY (col1))

CREATE UNIQUE INDEX IX1 ON myTable99 (col1)
WITH FILLFACTOR = 90 ON [PRIMARY]
GO

CREATE TRIGGER myTrigger ON myTable99
FOR INSERT, UPDATE
AS
UPDATE myTable99
SET myTable99.col2 = 'Expired'
FROM myTable99
INNER JOIN Inserted
ON Inserted.col1 = myTable99.col1
WHERE Inserted.col3 > GetDate()
GO

INSERT INTO myTable99 (Col2, col3)
SELECT 'Unexpired', '01/01/1990' UNION ALL
SELECT 'Unexpired', '01/01/1991' UNION ALL
SELECT 'Unexpired', '01/01/1992' UNION ALL
SELECT 'Unexpired', '01/01/1993'
GO

SELECT * FROM myTable99
GO

INSERT INTO myTable99 (Col2, col3)
SELECT 'Unexpired', '01/01/2045'

UPDATE myTable99
SET col3 = '10/01/2055'
WHERE col3 = '01/01/1990'
GO

SELECT * FROM myTable99
GO

DROP TRIGGER myTrigger
GO

DROP TABLE myTable99
GO




Brett

8-)

SELECT @@POST=NewId()

That's correct! It's an AlphaNumeric!
Go to Top of Page

mohdowais
Sheikh of Yak Knowledge

1456 Posts

Posted - 2003-09-02 : 12:25:48
You might be better off not storing the Status column in the table then. you could create a view, which has an additional computed column for status:

SELECT blah, blah1, ExpiryDate,
CASE WHEN ExpiryDate > GetDate() THEN 'Expired' ELSE 'Valid' END AS Status
FROM MyTable

Owais

P.S. Good to have you back, Brett, did you enjoy your vacation ? Oh, we missed your MOO-ing at Mr. Mist's 500 post bash



Make it idiot proof and someone will make a better idiot
Go to Top of Page
   

- Advertisement -