Solved

After Insert Trigger not Firing

Posted on 2011-03-09
5
550 Views
Last Modified: 2012-05-11
Hallo all

I have the trigger below..
After Insert of an Article : the trigger will fail to update the current Article.
It Only update an article before the current Transaction

i.e
Article 1 : - Time 45
Article 2 :- Time 50

If I run insert with Article 3, only the Article 1 and 2 will be updated the current Article that
Initated the transaction will not be update,
How can I get around this ?

Thanks in Advance

USE [Prise]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

      ALTER TRIGGER [dbo].[controlPrise]
            ON  [dbo].[Cheap_Preis_H]
                  AFTER INSERT
      AS
            

                  BEGIN
                        -- SET NOCOUNT ON added to prevent extra result sets from
                        -- interfering with SELECT statements.
                        SET NOCOUNT ON;                              
                        
                                    
                                    UPDATE [TST].[dbo].[Cheap_Preis_H]
                                    SET   State = 150
                                                
                                    WHERE State  =       50
                  END
0
Comment
Question by:ZURINET
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 35083932
you have to refer to INSERTED table to know which rows have been "updated".

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

      ALTER TRIGGER [dbo].[controlPrise] 
            ON  [dbo].[Cheap_Preis_H] 
                  AFTER INSERT
      AS 
            

                  BEGIN
                        -- SET NOCOUNT ON added to prevent extra result sets from
                        -- interfering with SELECT statements.
                        SET NOCOUNT ON;                              
                        
                                    
                                    UPDATE [TST].[dbo].[Cheap_Preis_H]
                                    SET   State = 150
                                                
                                    WHERE Primary_Key_Field in ( SELECT primary_key_field FROM INSERTED )
                  END

Open in new window

0
 

Author Comment

by:ZURINET
ID: 35092639
Hoi AngelIII
This solution did not solve my problem..

According to Profiler Log.. The real insert occurs after the trigger..

I am still looking for a solution
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35092769
let me show this test script and it's output:
USE TEMPDB
go
set nocount on
go
create table [dbo].[Cheap_Preis_H] ( Primary_Key_Field int, data varchar(10), state int )
go

      CREATE TRIGGER [dbo].[controlPrise]
            ON  [dbo].[Cheap_Preis_H]
                  AFTER INSERT
      AS
            

                 
                  BEGIN
                        -- SET NOCOUNT ON added to prevent extra result sets from
                        -- interfering with SELECT statements.
                        SET NOCOUNT ON;                              
                        
                                    
                                    UPDATE [dbo].[Cheap_Preis_H]
                                    SET   State = 150                                                
                                    WHERE Primary_Key_Field in ( SELECT primary_key_field FROM INSERTED )
                  END
go
insert into [dbo].[Cheap_Preis_H] (Primary_Key_Field, data, state ) values ( 1, 'row 1' , 10)
go
select * from [dbo].[Cheap_Preis_H] 
go
update [dbo].[Cheap_Preis_H] 
  set state = 10
where Primary_Key_Field = 1
go
select * from [dbo].[Cheap_Preis_H] 
go
insert into [dbo].[Cheap_Preis_H] (Primary_Key_Field, data ) values ( 2, 'row 2' )
go
select * from [dbo].[Cheap_Preis_H] 

go
drop table [dbo].[Cheap_Preis_H] 

Open in new window


result:
Primary_Key_Field	data	state
1	row 1	150

Primary_Key_Field	data	state
1	row 1	10

Primary_Key_Field	data	state
1	row 1	10
2	row 2	150

Open in new window


which seem to be the proof that the after insert trigger does update the state field for ONLY the row that has been inserted ...
0
 

Author Comment

by:ZURINET
ID: 35092803
Hi

I recorded the same sucess if I execute the code in Sql Analyser..
However if the Processe , i.e if the source file is Inserted or written by BizTalk server to the SQL DB
The query will not update the DB.

May be the problem lies on how the BizTalk Sql adapter is  inserting data into the DB.

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35092852
as long as the process would not do a BULK INSERT (aka from file), the triggers must all fire.
you might want to perform a sql profiler on the database to monitor the sql run against the db, to see what is happening (you shall also see the sql from the trigger running ...)
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how many extra RAM for SQL server is needed 23 48
CROSS APPLY 4 61
user defined date datatype in SQL Server- can it be overdone.. 6 37
Using this function 4 39
Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

761 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question