[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2327
  • Last Modified:

Simple Trigger Question

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
Cyber-Drugs
Asked:
Cyber-Drugs
  • 4
  • 3
  • 2
  • +2
3 Solutions
 
riaancorneliusCommented:
Not as far as I'm aware
0
 
r_a_j_e_s_hCommented:
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
 
Cyber-DrugsAuthor Commented:
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
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 
riaancorneliusCommented:
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
 
imran_fastCommented:
>>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
 
riaancorneliusCommented:
>> 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
 
imran_fastCommented:
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
 
Scott PletcherSenior DBACommented:
>> 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
 
Cyber-DrugsAuthor Commented:
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
 
Scott PletcherSenior DBACommented:
>> 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
 
riaancorneliusCommented:
>> 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
 
Cyber-DrugsAuthor Commented:
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now