问题
I need to change the value of a column when a record is updated, based on the value of another column in the same table. The legacy application updating the table cannot be re-coded to handle this.
The basic logic would be:
- If
DateShipped
is not null, setOrderLocation = 4
Hoping I can do this at the database level with an update trigger in SQL Server 2008 R2.
thank you.
回答1:
CREATE TRIGGER tr_OrderLocation_Update
ON TableName
FOR UPDATE,INSERT
BEGIN
SET NOCOUNT ON;
UPDATE t
SET t.OrderLocation = 4
FROM TableName t INNER JOIN inserted i
ON t.PK_Column = i.PK_Column --<-- what ever the primary key column
WHERE i.DateShipped IS NOT NULL
END
来源:https://stackoverflow.com/questions/30064311/use-a-trigger-in-sql-server-2008-r2-to-change-the-value-of-one-column-based-on-t