SQL Server更新后触发器未触发

时间:2014-11-22 12:05:10

标签: sql-server triggers

请帮助,我一直在尝试创建一个更新触发器,以便在使用特定值更新后将数据插入到特定的表中。 "接受&#34 ;.由于某种原因触发器没有触发



IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[TRGIU_CLEARING_SAMPLE_RESULT]') and OBJECTPROPERTY(id, N'IsTrigger') = 1)
DROP TRIGGER [dbo].[TRGIU_CLEARING_SAMPLE_RESULT]
GO

CREATE TRIGGER [dbo].[TRGIU_CLEARING_SAMPLE_RESULT] ON [dbo].[CLEARING_BATCH]               
AFTER INSERT                
AS                

IF UPDATE(QM_STATUS)                
BEGIN                
  Declare @QM_Status Varchar(12),                
          @QM_Status_Old Varchar(12),                
          @Submission_Pk Uniqueidentifier            
                
  SELECT @QM_Status = I.QM_STATUS,                
         @QM_Status_old = D.QM_STATUS,                
         @Submission_Pk = I.PK               
         --@Lab_used = LE.ID                            
    FROM CLEARING_BATCH CB                
    LEFT JOIN SAMPLE_RESULT SR ON CB.PK = SR.PPK                 
    JOIN INSERTED I ON SR.PK = I.PPK                 
    JOIN DELETED D ON I.PK = D.PK 

             
  IF(@QM_Status = 'Accepted' and (@QM_Status <> @QM_Status_old or @QM_Status_old is null))                
  
  
  begin

  INSERT INTO BATCH(PPK,BATCH_ID)

  SELECT CB.PK,CB.BATCH_ID
  FROM LABORATORY L
  JOIN CLEARING_BATCH CB ON L.PK = CB.PPK
  JOIN CLEARING_SAMPLE_RESULT CSR ON CB.PK = CSR.PPK
  LEFT JOIN BATCH B ON CB.PK = B.PPK
  WHERE B.PPK IS NULL
 

  UPDATE BATCH
  SET BATCH_ID = CB.BATCH_ID

  FROM BATCH B
  JOIN CLEARING_BATCH CB ON B.PPK = CB.PPK

  END
  end
  GO
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您有一个 AFTER INSERT 触发器

CREATE TRIGGER [dbo].[TRGIU_CLEARING_SAMPLE_RESULT] 
ON [dbo].[CLEARING_BATCH]               
AFTER INSERT                
AS               

会在插入新行的INSERT语句后触发 - 当您UPDATE某事时, NOT 会被触发!

如果您想捕获UPDATE操作,则需要使用 AFTER UPDATE 触发器:

CREATE TRIGGER [dbo].[TRGIU_CLEARING_SAMPLE_RESULT] 
ON [dbo].[CLEARING_BATCH]
AFTER UPDATE
AS
   ....