背景:如果在同一张表下采集,会造成堵塞
复制一张表
在触发器里写三个:insert、update、delete
USE [asrs37db] GO /****** Object: Trigger [dbo].[tr_delete_trffl] Script Date: 05/01/2023 21:03:46 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <Author,,Name> -- Create date: <Create Date,,> -- Description: <Description,,> -- ============================================= ALTER trigger [dbo].[tr_delete_trffl] on [dbo].[ASRS_TRFFL] AFTER DELETE AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; delete b from [dbo].[ASRS_TRFFL2] b,deleted d where b.UNIQUE_ID=d.UNIQUE_ID -- Insert statements for trigger here END
USE [asrs37db] GO /****** Object: Trigger [dbo].[tr_insert_ASRS_TRFFL] Script Date: 05/01/2023 21:04:15 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER trigger [dbo].[tr_insert_ASRS_TRFFL] on [dbo].[ASRS_TRFFL] for insert as set xact_abort on insert ASRS_TRFFL2 (OPNO,sto_in_out,PROD_DATE,PROD_LINE,FIRST_IN_DATE,PROD_DATETIME,MATNR,STNO,TRAN_TIME,UNIQUE_ID) select OPNO,sto_in_out,PROD_DATE,PROD_LINE,FIRST_IN_DATE,PROD_DATETIME,MATNR,STNO,TRAN_TIME,UNIQUE_ID from inserted
USE [asrs37db] GO /****** Object: Trigger [dbo].[tr_update_trffl] Script Date: 05/01/2023 21:04:44 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <Author,,Name> -- Create date: <Create Date,,> -- Description: <Description,,> -- ============================================= ALTER trigger [dbo].[tr_update_trffl] on [dbo].[ASRS_TRFFL] AFTER UPDATE AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; update b set UNIQUE_ID=i.UNIQUE_ID,OPNO=i.OPNO,sto_in_out=i.STO_IN_OUT,PROD_DATE=i.PROD_DATE,PROD_LINE=i.PROD_LINE,FIRST_IN_DATE=i.FIRST_IN_DATE,PROD_DATETIME=i.PROD_DATETIME,MATNR=i.MATNR,STNO=i.STNO,TRAN_TIME=i.TRAN_TIME from [dbo].[ASRS_TRFFL2] b,inserted i where b.UNIQUE_ID=i.UNIQUE_ID -- Insert statements for trigger here END
上一篇:关于异动记录打开要十来分钟的优化
下一篇:没有了