SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Trigger and commit


Trigger and commit

Author
Message
Krishna1
Krishna1
Right there with Babe
Right there with Babe (784 reputation)Right there with Babe (784 reputation)Right there with Babe (784 reputation)Right there with Babe (784 reputation)Right there with Babe (784 reputation)Right there with Babe (784 reputation)Right there with Babe (784 reputation)Right there with Babe (784 reputation)

Group: General Forum Members
Points: 784 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
Krishna1
Krishna1
Right there with Babe
Right there with Babe (784 reputation)Right there with Babe (784 reputation)Right there with Babe (784 reputation)Right there with Babe (784 reputation)Right there with Babe (784 reputation)Right there with Babe (784 reputation)Right there with Babe (784 reputation)Right there with Babe (784 reputation)

Group: General Forum Members
Points: 784 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
Sean Lange
Sean Lange
SSC Guru
SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)

Group: General Forum Members
Points: 59726 Visits: 17947
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 Modens 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)
Krishna1
Krishna1
Right there with Babe
Right there with Babe (784 reputation)Right there with Babe (784 reputation)Right there with Babe (784 reputation)Right there with Babe (784 reputation)Right there with Babe (784 reputation)Right there with Babe (784 reputation)Right there with Babe (784 reputation)Right there with Babe (784 reputation)

Group: General Forum Members
Points: 784 Visits: 556
Dear Sean

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

Regards
Krishna
Sean Lange
Sean Lange
SSC Guru
SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)

Group: General Forum Members
Points: 59726 Visits: 17947
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 Modens 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)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search