Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Transactions in general

 
   Database Help (Home) -> Programming RSS
Next:  how to remove space  
Author Message
Joachim Hofmann

External


Since: Apr 02, 2004
Posts: 12



(Msg. 1) Posted: Mon Nov 27, 2006 3:32 pm
Post subject: Transactions in general
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Hello,

I have an sp1 which calls sp2 which calls sp3.
Is it correct that if I only need to surround the code of sp1 with BEGIN / END
TRANSACTION to run all the code in a Transaction?

Thank You

Joachim

 >> Stay informed about: Transactions in general 
Back to top
Login to vote
Roy Harvey

External


Since: May 03, 2006
Posts: 270



(Msg. 2) Posted: Mon Nov 27, 2006 3:32 pm
Post subject: Re: Transactions in general [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Yes.

Roy Harvey
Beacon Falls, CT

On Mon, 27 Nov 2006 15:32:27 +0100, Joachim Hofmann
wrote:

>Hello,
>
>I have an sp1 which calls sp2 which calls sp3.
>Is it correct that if I only need to surround the code of sp1 with BEGIN / END
>TRANSACTION to run all the code in a Transaction?
>
>Thank You
>
>Joachim

 >> Stay informed about: Transactions in general 
Back to top
Login to vote
Uri Dimant

External


Since: Aug 24, 2003
Posts: 1744



(Msg. 3) Posted: Mon Nov 27, 2006 4:36 pm
Post subject: Re: Transactions in general [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Joachim
CREATE PROCEDURE BigOne
AS
DECLARE @err integer
BEGIN TRANSACTION
EXEC @err = sp1
SELECT @err = coalesce(nullif(@err, 0, @@error)
IF @err <> 0 BEGIN ROLLBACK TRANSACTION RETURN 1 END
EXEC @err = sp2
SELECT @err = coalesce(nullif(@err, 0, @@error)
IF @err <> 0 BEGIN ROLLBACK TRANSACTION RETURN 1 END
EXEC @err = sp3
SELECT @err = coalesce(nullif(@err, 0, @@error)
IF @err <> 0 BEGIN ROLLBACK TRANSACTION RETURN 1 END
EXEC @err = sp4
SELECT @err = coalesce(nullif(@err, 0, @@error)
IF @err <> 0 BEGIN ROLLBACK TRANSACTION RETURN 1 END
COMMIT TRANSACION
GO



"Joachim Hofmann" wrote in message

> Hello,
>
> I have an sp1 which calls sp2 which calls sp3.
> Is it correct that if I only need to surround the code of sp1 with BEGIN /
> END
> TRANSACTION to run all the code in a Transaction?
>
> Thank You
>
> Joachim
 >> Stay informed about: Transactions in general 
Back to top
Login to vote
Joachim Hofmann

External


Since: Apr 02, 2004
Posts: 12



(Msg. 4) Posted: Mon Nov 27, 2006 6:32 pm
Post subject: Re: Transactions in general [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

I understand the approach of Uri immediately returning the error code
to the caller.
However the short answer "Yes" from Roy does not seem to be correct.


So I did a test script by myself to understand it: sp1 calls sp2, sp2 calls
sp3. Each sp does an INSERT INTO Testtabelle1.
sp3 causes a constraint violation by an INSERT command. But all other INSERTs
remain in spite of a Transaction surrounding all the code in sp1. The @@ERROR
of sp3 is "too far away" from sp1.
>>>>>

use pubs
GO

ALTER PROCEDURE sp1
AS
if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[Testtabelle1]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Testtabelle1]

CREATE TABLE [dbo].[Testtabelle1] (
[pk] [int] IDENTITY (1, 1) NOT NULL ,
[name] [char] (10) COLLATE Latin1_General_CI_AS NOT NULL
) ON [PRIMARY]


BEGIN transaction t1
INSERT INTO testtabelle1
(name)
VALUES
('von_sp1')
EXECUTE sp2
IF @@ERROR <> 0
BEGIN
/* print 'rollback' */
ROLLBACK transaction t1
END
ELSE
BEGIN
/* print 'commit' */
COMMIT transaction t1
END
GO

ALTER PROCEDURE sp2
AS
INSERT INTO testtabelle1
(name)
VALUES
('von_sp2') -- ok
execute sp3
GO

ALTER PROCEDURE sp3
AS
INSERT INTO testtabelle1
(name)
VALUES
(NULL) -- violation
INSERT INTO testtabelle1
(name)
VALUES
('von_sp3')
GO

<<<<<

Joachim
 >> Stay informed about: Transactions in general 
Back to top
Login to vote
Tibor Karaszi

External


Since: Jan 29, 2004
Posts: 1559



(Msg. 5) Posted: Mon Nov 27, 2006 6:50 pm
Post subject: Re: Transactions in general [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

You need robust error handling. What you are doing now is similar to:

BEGIN TRAN
INSERT
INSERT
INSERT
COMMIT

If INSERT #3 fails, the transaction still commits, so even though the inserts are in the same
transaction, because of lack of error handling, one insert fails and you are left with two performed
operations and one failed operation.

Your checking of @@ERROR after execution p2 doesn't help since you need to capture @@ERROR
immediately after the INSERT inside the inner procedures. So, communicate the failure of the inner
procedures using a return code and have the outer most procedure check against that return code. Or,
investigate the new TRY/CATCH elements available in 2005.



--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/


"Joachim Hofmann" wrote in message

>
> I understand the approach of Uri immediately returning the error code
> to the caller.
> However the short answer "Yes" from Roy does not seem to be correct.
>
>
> So I did a test script by myself to understand it: sp1 calls sp2, sp2 calls
> sp3. Each sp does an INSERT INTO Testtabelle1.
> sp3 causes a constraint violation by an INSERT command. But all other INSERTs
> remain in spite of a Transaction surrounding all the code in sp1. The @@ERROR
> of sp3 is "too far away" from sp1.
> >>>>>
>
> use pubs
> GO
>
> ALTER PROCEDURE sp1
> AS
> if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Testtabelle1]') and
> OBJECTPROPERTY(id, N'IsUserTable') = 1)
> drop table [dbo].[Testtabelle1]
>
> CREATE TABLE [dbo].[Testtabelle1] (
> [pk] [int] IDENTITY (1, 1) NOT NULL ,
> [name] [char] (10) COLLATE Latin1_General_CI_AS NOT NULL
> ) ON [PRIMARY]
>
>
> BEGIN transaction t1
> INSERT INTO testtabelle1
> (name)
> VALUES
> ('von_sp1')
> EXECUTE sp2
> IF @@ERROR <> 0
> BEGIN
> /* print 'rollback' */
> ROLLBACK transaction t1
> END
> ELSE
> BEGIN
> /* print 'commit' */
> COMMIT transaction t1
> END
> GO
>
> ALTER PROCEDURE sp2
> AS
> INSERT INTO testtabelle1
> (name)
> VALUES
> ('von_sp2') -- ok
> execute sp3
> GO
>
> ALTER PROCEDURE sp3
> AS
> INSERT INTO testtabelle1
> (name)
> VALUES
> (NULL) -- violation
> INSERT INTO testtabelle1
> (name)
> VALUES
> ('von_sp3')
> GO
>
> <<<<<
>
> Joachim
 >> Stay informed about: Transactions in general 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Nested Transactions in c# - Hi, I want to implement nested transactions in C#. When I write BEGIN TRANSACTION inside another BEGIN TRANSACTION in an SQL Script it works fine. But when I call BeginTransaction() inside another BeginTransaction() in a c# code on same connection objec...

repeated transactions - I have a table where I have one transaction number(row) and five data items. If all entries are correct then the five values are OK, but if there are two entries one manual and one automatic for one transaction the row gets repeated. The repeated..

Open transactions - Hello everyone, we have a database we use for an aspnet application that, sometimes, gives us problems. Checking my applications' logs I found many Timeout Expired problems, looks like a transaction is blocking some tables... I checked all my source....

Nested Transactions - What is the database behaviour in case of nested transactions? Assume that we have a transaction that starts from the code which in turn calls a stored procedure that starts and commits a transaction, the code then rolls back the transaction. Are all..

"general" backup strategy - I am curious about general DBA's backup strategy. Do you backup directly to the network drive? or backup directly to the same server that hosts sql server and have window or other 3rd party software backup to a network drive or tape? When you do..
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]