一种方法是代替触发器,它允许所有更新通过,除了一旦它等于 1 就改变位域的任何东西。在这种情况下,它允许除了位域改变之外的所有更新。
CREATE TRIGGER OneWayBitChange
ON YourTable
INSTEAD OF UPDATE
AS
BEGIN
UPDATE YourTable SET /* update all fields from original update except bitfield */
Field1 = i.Field1,
Field2 = i.Field2,
Field3 = i.Field3
FROM YourTable
INNER JOIN Inserted i ON i.PrimaryKey = YourTable.PrimaryKey
UPDATE YourTable SET /* update bitfield only if it's not already a 1 */
BitField = i.BitField
FROM YourTable
INNER JOIN Inserted i ON i.PrimaryKey = YourTable.PrimaryKey
WHERE IsNull(YourTable.BitField,0) < 1
END
GO
以上将允许所有更新在从 1 变为其他任何值(0 或 null)时接受一个字段。
如果您想在尝试对位字段进行尝试时取消对该行的任何更新,您可以像这样修改正文:
UPDATE YourTable SET /* update all except bitfield changes from 1 to 0 */
Field1 = i.Field1,
Field2 = i.Field2,
Field3 = i.Field3,
BitField = i.BitField
FROM YourTable
INNER JOIN Inserted i ON i.PrimaryKey = YourTable.PrimaryKey
WHERE IsNull(BitField,0) = 0 OR IsNull(i.BitField,0) = 1