我想更新包含序列号(字符串)的行,仅当位为0(也称为false)时更新布尔值(位)和日期(日期)。我希望在服务器端完成此操作,以便用户在该行设置为false后无法更改该行。否则,该行可自由编辑,而该行设置为1(也称为true)。
我已经尝试编写一个触发器来为我执行此操作,但是我无法在更新之前解析它以确定是否应该写入该行。
例如,
第1行:SN = 123456,TorF = 1,日期= 01-31-2015 ::这可以为更新的日期进行编辑/更新
第2行:SN = 654321,TorF = 0,日期= 03-31-2015 ::这将无法更改,并会向用户抛出错误
编辑: 这是我试过的
CREATE TRIGGER DisallowOnFailEdit
ON myTable
INSTEAD OF UPDATE
AS
IF EXISTS(SELECT myColumn FROM LoopBackCable WHERE myColumn = 0)
THROW 50000, 'Cable has been permanently retired', 1
ROLLBACK TRANSACTION;
答案 0 :(得分:0)
您可以检查已删除的表格:
CREATE TRIGGER DisallowOnFailEdit
ON myTable
FOR UPDATE
AS
IF EXISTS(SELECT * FROM DELETED WHERE myColumn = 0)
BEGIN
THROW 50000, 'Cable has been permanently retired', 1
ROLLBACK TRANSACTION;
END
或者相反而不是触发器:
CREATE TRIGGER DisallowOnFailEdit
ON myTable
INSTEAD OF UPDATE
AS
IF EXISTS(SELECT * FROM INSERTED i
JOIN myTable m on i.PK = m.PK WHERE m.myColumn = 0)
BEGIN
THROW 50000, 'Cable has been permanently retired', 1
ROLLBACK TRANSACTION;
END
UPDATE m
SET Col1 = i.Col1,
Col2 = i.Col2
....
FROM INSERTED i
JOIN myTable m on i.PK = m.PK