Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

After insert trigger not firing Expand / Collapse
Author
Message
Posted Tuesday, July 30, 2013 6:24 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, August 3, 2013 8:37 AM
Points: 10, Visits: 35
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
Post #1478950
Posted Tuesday, July 30, 2013 6:28 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:20 AM
Points: 12,927, Visits: 32,332
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1478953
Posted Tuesday, July 30, 2013 6:46 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, August 3, 2013 8:37 AM
Points: 10, Visits: 35
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?
Post #1478964
Posted Tuesday, July 30, 2013 6:47 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, August 3, 2013 8:37 AM
Points: 10, Visits: 35
Lowell,

Sorry, didn't see your inserted code? Would using the INSERTED table get the trigger to fire each time?
Post #1478965
Posted Tuesday, July 30, 2013 7:01 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:20 AM
Points: 12,927, Visits: 32,332
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1478979
Posted Tuesday, July 30, 2013 8:10 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, August 3, 2013 8:37 AM
Points: 10, Visits: 35
Lowell,

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

Kevin
Post #1479022
Posted Tuesday, July 30, 2013 10:04 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:20 AM
Points: 12,927, Visits: 32,332
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1479090
Posted Tuesday, July 30, 2013 10:57 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, August 3, 2013 8:37 AM
Points: 10, Visits: 35
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
Post #1479108
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse