Solved

Simple Trigger Question

Posted on 2006-07-10
12
2,314 Views
Last Modified: 2008-01-09
Hi guys,

I was just wanted to find out...

In Oracle Database, you can create a AFTER COMMIT Trigger, so a trigger is fired AFTER a group of Insert Queries are made.

Does MS SQL have a similar feature?

Cheers!
0
Comment
Question by:Cyber-Drugs
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 9

Expert Comment

by:riaancornelius
Comment Utility
Not as far as I'm aware
0
 
LVL 3

Expert Comment

by:r_a_j_e_s_h
Comment Utility
Hope the following details will help you,


You can specify one of two options to control when a trigger fires:

AFTER triggers fire after the triggering action (INSERT, UPDATE, or DELETE) and after any constraints are processed. You can request AFTER triggers by specifying either the AFTER or FOR keywords. Because the FOR keyword has the same effect as AFTER, triggers with the FOR keyword are also classified as AFTER triggers.


INSTEAD OF triggers fire in place of the triggering action and before constraints are processed.
Each table or view can have one INSTEAD OF trigger for each triggering action (UPDATE, DELETE, and INSERT). A table can have several AFTER triggers for each triggering action.

Examples
A. Use the INSTEAD OF trigger to replace the standard triggering action
CREATE TRIGGER TableAInsertTrig ON TableA
INSTEAD OF INSERT
AS ...

B. Use the AFTER trigger to augment the standard triggering action
CREATE TRIGGER TableBDeleteTrig ON TableB
AFTER DELETE
AS ...

C. Use the FOR trigger to augment the standard triggering action
-- This statement uses the FOR keyword to generate an AFTER trigger.
CREATE TRIGGER TableCUpdateTrig ON TableC
FOR UPDATE
AS ...

0
 
LVL 4

Author Comment

by:Cyber-Drugs
Comment Utility
Thanks for the replies,

Is there no third party code I can add to MS SQL to impliment an AFTER COMMIT trigger ability?

Cheers!
0
 
LVL 9

Assisted Solution

by:riaancornelius
riaancornelius earned 167 total points
Comment Utility
not that I know of, but you can maybe create a seperate table into which you insert a value after the commit, and put a trigger on that table. Dunno how you can check whether a commit happened though.
0
 
LVL 28

Expert Comment

by:imran_fast
Comment Utility
>>AFTER a group of Insert Queries are
Yes it does it you have to run the statement in a batch

and then you can specify the cursor inside the trigger on inserted system table to check each and every record

use standard trigger statement
CREATE TRIGGER TableCUpdateTrig ON TableC
FOR INSERT
AS ...

begin transaction
insert statemnt 1
insert statement 2
commit
go
0
 
LVL 9

Expert Comment

by:riaancornelius
Comment Utility
>> use standard trigger statement
CREATE TRIGGER TableCUpdateTrig ON TableC
FOR INSERT
AS ...

begin transaction
insert statemnt 1             //1
insert statement 2           //2
commit                          //3
go

That will fire trigger at point 1 and 2, and not at 3.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 28

Assisted Solution

by:imran_fast
imran_fast earned 167 total points
Comment Utility
Sorry for previous post rianncornelius is right

It is less common for INSERT triggers, because the basic INSERT statement adds only a single row. However, because an INSERT trigger can be fired by an INSERT INTO (table_name) SELECT statement,

so what you have to do in that case is to insert all the rows in a temp table or table variable and then fire

INSERT INTO (table_name) SELECT from temp table to follow that approch and use the cursor inside the trigger on inserted table to fetch each record.
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 166 total points
Comment Utility
>> Does MS SQL have a[n] [AFTER COMMIT INSERT trigger]? <<

NO.  The trigger is *always* part of the transaction that the initial INSERT (| UPDATE | DELETE) is in.  If the trigger has a serious failure, the INSERT(s) will be rolled back automatically.

Probably the closest you could get to simulating an "AFTER COMMIT" trigger is to have the trigger start a job, with the job having a time-delay (WAITFOR), hopefully providing enough delay for the COMMIT to have taken place before the rest of the code in the job runs.
0
 
LVL 4

Author Comment

by:Cyber-Drugs
Comment Utility
Hi guys,

Thanks for all the replies, I've ended up going for this solution:

AFTER INSERT Trigger which reads the first field of the record to see if it's the last record in the batch
-- If false, do nothing
-- If true, fire a stored procedure which was meant to be fired on the AFTER COMMIT

Not sure how to close this question, split the points between everyone or what? All answers were very good, but I ended up using in this instance the one I just mentioned...
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
>> AFTER INSERT Trigger which reads the first field of the record to see if it's the last record in the batch <<

That will work only if the rows are inserted one at a time.  Remember that a trigger only fires once *per statement* in SQL Server, no matter how many rows are inserted (|updated|deleted) at one time.
0
 
LVL 9

Expert Comment

by:riaancornelius
Comment Utility
>> Not sure how to close this question, split the points between everyone or what? All answers were very good, but I ended up using in this instance the one I just mentioned...

As a general rule, you award points for the 'correct' answer, but if other answers broadened your knowledge, or was worthwhile to you for whatever reason, you can award points for those as well.

So basically, feel free to split points, just leave a note as to what solution you used so people looking at the solution afterwards can see how to best solve the problem.
0
 
LVL 4

Author Comment

by:Cyber-Drugs
Comment Utility
ScottPletcher - It's C code, which does a loop making hundreds of Insert statements, so it should work fine.

riaancornelius  - Well as you said, I have learned a few additional things in this question of mine, even though I didn't go for any of the listed solutions in the end.


I am going to split the points evenly, but for anyone who comes across this problem, look at my last comment to see what solution I chose to take.

Cheers guys!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

763 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

12 Experts available now in Live!

Get 1:1 Help Now