如果触发器中的语句取决于插入的值

时间:2014-10-14 17:12:43

标签: sql-server if-statement triggers

如果我有SQL Server触发器,我可以执行不同的更新语句,具体取决于插入/更新记录的值吗?

IF inserted.Make = 'FORD'
  BEGIN
    UPDATE that depends on Ford
  END
ELSE
  BEGIN
    UPDATE that depends on other Make
  END

我认为这是正确的,但我得到The multi-part identifier "inserted.Make" could not be bound

1 个答案:

答案 0 :(得分:2)

此解决方案也可在this link处获得。

CREATE TRIGGER 
    [dbo].[SystemParameterInsertUpdate]
ON 
    [dbo].[SystemParameter]
FOR INSERT, UPDATE 
AS
  BEGIN
SET NOCOUNT ON

  If (SELECT Attribute FROM INSERTED) LIKE 'NoHist_%'
  Begin
      Return
  End

  INSERT INTO SystemParameterHistory 
  (
    Attribute,
    ParameterValue,
    ParameterDescription,
    ChangeDate
  )
SELECT
  Attribute,
  ParameterValue,
  ParameterDescription,
  ChangeDate
FROM Inserted AS I
END