0

在 Sql Server 2008 中,有没有办法设置只能使用触发器更改的字段?

例子:

当您创建一条记录时,它被设置为 NULL,然后由触发器更新为例如 1。用户不应将其设置为 NULL 以外的任何值。然后当它更新时,触发器会将值设置为 2。

4

2 回答 2

1
create table tmp (a int primary key, b int)
GO

create trigger tr_insupd_tmp on tmp
instead of insert, update
as
    if not exists(select * from deleted)
        -- Process Insert
        insert into tmp
            select a, 1
            from inserted
    else
        -- Process Update
        update tmp
            set b = 2
        from tmp t
        inner join inserted i on t.a = i.a
GO
于 2013-02-18T11:28:07.543 回答
0

您是否考虑过使用计算列。如果您希望此列中的数据仅受数据库逻辑控制,那么计算列可能是可行的方法。

于 2013-02-18T15:47:28.450 回答