Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Trigger and commit Expand / Collapse
Author
Message
Posted Tuesday, April 22, 2014 5:46 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Sunday, April 27, 2014 7:55 PM
Points: 218, Visits: 556
Dear All

I have a update trigger. In this trigger I need to insert few records in 3 tables. If error comes in any of these inserts then previous inserts to get committed.

This trigger was written in Sybase and it was possible to create transaction and commit the transactions.

Can somebody help me in doing the same in SQL2012


Thanks and best regards
Krishna

Post #1564065
Posted Tuesday, April 22, 2014 7:18 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Sunday, April 27, 2014 7:55 PM
Points: 218, Visits: 556
I need the insert of table a and c to be successful even though insert to be fails.


code sample

drop table a
go
create table a
(a1 integer identity, a2 integer)
go
drop table b
go
create table b
(b1 integer identity, b2 integer)
go
drop table c
go
create table c
(c1 integer identity, c2 integer)
go


CREATE TRIGGER [TRaInsert]
ON [a]
FOR INSERT
AS
BEGIN TRY

begin transaction c
begin try
begin transaction b
insert into b (b2) values(1)
select 1/0 -- error created
commit transaction b
end try
begin catch
print 'catch of transaction b'
rollback transaction b
end catch

insert into c (c2) values(1)
commit transaction c
end try
begin catch
select ERROR_MESSAGE()
end catch
go
Post #1564071
Posted Wednesday, April 23, 2014 7:24 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, October 17, 2014 2:15 PM
Points: 13,077, Visits: 12,523
Krishna1 (4/22/2014)
I need the insert of table a and c to be successful even though insert to be fails.

code sample 

drop table a
go
create table a
(a1 integer identity, a2 integer)
go
drop table b
go
create table b
(b1 integer identity, b2 integer)
go
drop table c
go
create table c
(c1 integer identity, c2 integer)
go


CREATE TRIGGER [TRaInsert]
ON [a]
FOR INSERT
AS
BEGIN TRY

begin transaction c
begin try
begin transaction b
insert into b (b2) values(1)
select 1/0 -- error created
commit transaction b
end try
begin catch
print 'catch of transaction b'
rollback transaction b
end catch

insert into c (c2) values(1)
commit transaction c
end try
begin catch
select ERROR_MESSAGE()
end catch
go



You can't do this like this at all. There is no such thing as nested transactions, they are a complete farce. An insert statement is inside of an implicit transaction. As soon as you issue a rollback anywhere it will rollback the outer most transaction and reset @@trancount to 0. When you start a nested transaction all that actually happens is @@trancount is increased by 1.

You could probably wrap each of your inserts inside of a try catch but no transactions. I realize your example was simplified for posting but why do you have to do so much work inside of a trigger? Also, you need to be sure that your real trigger logic is set based so it can handle multiple row inserts. Keep in mind that triggers can cause lots of performance issues in sql server if they are not setup correctly.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1564257
Posted Thursday, April 24, 2014 7:37 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Sunday, April 27, 2014 7:55 PM
Points: 218, Visits: 556
Dear Sean

Thank for reply. Is there another way I can achieve the same logic?

Regards
Krishna
Post #1564936
Posted Monday, April 28, 2014 7:09 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, October 17, 2014 2:15 PM
Points: 13,077, Visits: 12,523
Krishna1 (4/24/2014)
Dear Sean

Thank for reply. Is there another way I can achieve the same logic?

Regards
Krishna


There are several ways to achieve what you are trying to do. Some of it depends on what you are really trying to do. If the scenario you described is accurate then they way I suggested is the simplest way to accomplish that in my opinion. Is there some reason you want to do it differently?


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1565564
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse