Is there a way to store the value of the field only once?

advertisements

Is there any way to store (while inserting) a value on column which can never be changed ?

Like example :

insert to DB row "a,b,c,x,s,X" - and the X *wont* be able to change. (however , if the whole row is deleted - its fine"


You can write a trigger before updating or after updating ( http://msdn.microsoft.com/en-us/library/ms188601.aspx ) the rows to check whether it is going to change or not. Here is an example trigger after update , it will rollback transaction if column modified.

CREATE TRIGGER tr_update on YourTable AFTER UPDATE AS
    IF UPDATE(YourColumn)
    BEGIN
        RAISERROR ('cannot change yourColumn', 16, 1)
        ROLLBACK TRAN
        RETURN
    END
GO