After insert trigger not firing

  • I'm a newbie at SQL, so please go easy, LOL. I have an SQL Express 2008 R2 database that contains 2 tables, one called StudentID and the other called StaffID. These tables are used by a third party software. I have created the following AFTER INSERT trigger for both tables that creates the next barcode values. The trigger in the StudentID table fires every time a record is inserted, but the trigger in the StaffID will fire once and then stop firing after SQL server restart. I can't understand why this is happening since the triggers are same just in 2 different tables.

    USE [GroupTables]

    GO

    /****** Object: Trigger [dbo].[AddBarCode] Script Date: 07/15/2013 08:29:07 ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    -- =============================================

    -- Author:<Tager, Kevin>

    -- Create date: <7/13/2013>

    -- Description:<Trigger to generate next barcode values on insert>

    -- =============================================

    CREATE TRIGGER [dbo].[AddBarCode]

    ON [dbo].[StudentID]

    AFTER INSERT

    AS

    BEGIN

    -- SET NOCOUNT ON added to prevent extra result sets from

    -- interfering with SELECT statements.

    SET NOCOUNT ON;

    -- Update the barcode numbers of the most recently Inserted Record

    UPDATE [dbo].[StudentID]

    SET [FoodBarcode] = [dbo].[StudentID].[AI] + 3289,

    [LibraryBarcode] = [dbo].[StudentID].[AI] + 880101800707

    WHERE [dbo].[StudentID].[AI] = @@Identity

    END

    GO

  • your trigger is very close;

    inside a trigger, isntead of using @@identity, you wnat to use the virtual tables INSERTED (or DELETED, where approriate);

    that virtual table allows oyu to handle multiple rows at teh same time, and already has teh new values generated from any identity() columns, calculated columns, etc.

    USE [GroupTables]

    GO

    /****** Object: Trigger [dbo].[AddBarCode] Script Date: 07/15/2013 08:29:07 ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    -- =============================================

    -- Author: <Tager, Kevin>

    -- Create date: <7/13/2013>

    -- Description:<Trigger to generate next barcode values on insert>

    -- =============================================

    CREATE TRIGGER [dbo].[AddBarCode]

    ON [dbo].[StudentID]

    AFTER INSERT

    AS

    BEGIN

    -- SET NOCOUNT ON added to prevent extra result sets from

    -- interfering with SELECT statements.

    SET NOCOUNT ON;

    -- Update the barcode numbers of the most recently Inserted Record

    UPDATE [dbo].[StudentID]

    SET [FoodBarcode] = [dbo].[StudentID].[AI] + 3289,

    [LibraryBarcode] = [dbo].[StudentID].[AI] + 880101800707

    FROM INSERTED --INSERTED and DELETED are virtual tables, inside a trigger, with the new/old values

    WHERE [dbo].[StudentID].[AI]= INSERTED.[AI] --[AI] is the identity of the table/PK

    END

    GO

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Lowell,

    Thanks for the reply. Since I"m knew to SQL, I'm unfamiliar with how to work worth the inserted table. How would I modify my code to use it? I"m still unclear as to why the trigger, the way I wrote it, isn't firing on the one table, but is on the other. Any reason why it wouldn't fire?

  • Lowell,

    Sorry, didn't see your inserted code? Would using the INSERTED table get the trigger to fire each time?

  • ktager (7/30/2013)


    Lowell,

    Sorry, didn't see your inserted code? Would using the INSERTED table get the trigger to fire each time?

    basically, we are updating the current table from INSERTED, so that we limit the affected rows to only those in teh update, and not every row in the table.

    :

    UPDATE [dbo].[StudentID]

    SET [FoodBarcode] = [dbo].[StudentID].[AI] + 3289,

    [LibraryBarcode] = [dbo].[StudentID].[AI] + 880101800707

    FROM INSERTED --INSERTED and DELETED are virtual tables, inside a trigger, with the new/old values

    WHERE [dbo].[StudentID].[AI]= INSERTED.[AI] --[AI] is the identity of the table/PK

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Lowell,

    thanks, I will update the trigger to use the INSERTED table and retest to see if the trigger fires every time.

    Kevin

  • ktager (7/30/2013)


    Lowell,

    thanks, I will update the trigger to use the INSERTED table and retest to see if the trigger fires every time.

    Kevin

    the trigger always fires every time, it's just it might not update the data you expect it to.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Lowell,

    The problem I was having with the Staff table was that every time a record was added and saved, the 2 generated fields by the trigger were blank, which to me means the trigger wasn't firing.

    I'm going to setup a test environment that hopefully duplicates my customers environment and see if I can duplicate the issue. If I can't, then I'm going to assume it must be an issue with the SQL 2008 express on their server.

    thanks again for the help, much aprreciated.

    Kevin

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply