Solved

After Insert Trigger not Firing

Posted on 2011-03-09
5
514 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
  • 3
  • 2
5 Comments
 
LVL 142

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 142

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 142

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now