简体   繁体   中英

Sql Server Trigger After Insert

I have an existing Traffic Management system (Not designed by me) and want to set-up an auto email for proof of delivery - we have application that sends electronic signature etc. to our web server and updates in a few different tables the relevant information.

I want to now create a SP that sends an email to customer to give delivery details. I have created a trigger on one table where the status is given and the signature details ie name and gps location.

USE [Transport_Comp1]
GO
/****** Object:  Trigger [dbo].[TRG_InsertPodEmail2]    Script Date: 02/22/2016 23:18:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbo].[TRG_InsertPodEmail2]
   ON  [dbo].[DscPalletDetails]
   AFTER INSERT
AS 
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
    insert into dbo.Pod2Email ([dwDscPalletDetailsId]
      ,[dwPalletIdFK]
      ,[dwJobItemIdFK]
      ,[dwLegIdFK]
      ,[dwDscStatusIdFK]
      ,[szDscStatusCode]
      ,[szNotes]
      ,[bIsCollection]
      ,[dtUpdateDate]
      ,[szScannedBarcode]
      ,[dwSignatureIdFK]
      ,[dwScanStatusIdFk]
      ,[nScanRef]
      ,[szPalletNum])
    SELECT [dwDscPalletDetailsId]
      ,[dwPalletIdFK]
      ,[dwJobItemIdFK]
      ,[dwLegIdFK]
      ,[dwDscStatusIdFK]
      ,[szDscStatusCode]
      ,[szNotes]
      ,[bIsCollection]
      ,[dtUpdateDate]
      ,[szScannedBarcode]
      ,[dwSignatureIdFK]
      ,[dwScanStatusIdFk]
      ,[nScanRef]
      ,[szPalletNum]
      FROM INSERTED
    -- Insert statements for trigger here

END

Am I able to extend the PodEmail table to include data from another table ie account information?

I wanted to create a table that i can use as a basis for a cursor to loop thru records sending email to email from account table - is this the way to go or am i way off base?

Any pointers really appreciated

BR

Paul

Your approach is reasonable provided you keep the trigger simple and efficient, in particular:

  1. Your use of set nocount on is good practice as some applications, depending on how they are written can be confused by the multiple row counts being reported back to them. The triggering insert will either return a proper row count or no row count based on the prior setting of rowcount . This setting automatically reverts to its previous value when the trigger terminates (as it does with stored procedures generally).
  2. As @HGLEM points out in the comments above you can join inserted with other tables to get more columns for the insert into DML.
  3. Keep your trigger as a series of multiple row DML statements. NEVER use a cursor in a trigger.
  4. Be careful of joining with busy tables that may have long transactions with long running locks to go along with them. This can lead to bad performance of the trigger and even result in deadlocks with the possibility of the completed but uncommitted insert being rolled back as a deadlock victim.
  5. I would consider not running a cursor directly on the target insert into table but instead selecting the rows to process by the "email" cursor into a temp table (maybe even a table variable) and running the cursor over that table so as to avoid even the possibility of long running locks on the target insert into table.

Good luck with your project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM