Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


After insert trigger not firing


After insert trigger not firing

Author
Message
ktager
ktager
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
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
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14955 Visits: 38948
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!

ktager
ktager
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
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?
ktager
ktager
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 35
Lowell,

Sorry, didn't see your inserted code? Would using the INSERTED table get the trigger to fire each time?
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14955 Visits: 38948
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!

ktager
ktager
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
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
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14955 Visits: 38948
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!

ktager
ktager
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search