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


Whats wrong with it, Transaction mismatch.. Please help.


Whats wrong with it, Transaction mismatch.. Please help.

Author
Message
ashok84.kr
ashok84.kr
Right there with Babe
Right there with Babe (780 reputation)Right there with Babe (780 reputation)Right there with Babe (780 reputation)Right there with Babe (780 reputation)Right there with Babe (780 reputation)Right there with Babe (780 reputation)Right there with Babe (780 reputation)Right there with Babe (780 reputation)

Group: General Forum Members
Points: 780 Visits: 677
IF OBJECT_ID('test_parent_table') IS NOT NULL
DROP TABLE test_parent_table
CREATE TABLE test_parent_table
( id INT,
name VARCHAR(30)
)
GO

IF OBJECT_ID('DataError') IS NOT NULL
DROP TABLE DataError
CREATE TABLE DataError
( ErrorNo INT,
ErrorDesc VARCHAR(1000),
ErrorProc VARCHAR(100),
ErrorLineNumber INT,
ErrorDateTime DATETIME2(7)
)
GO

IF OBJECT_ID('ThrowError') IS NOT NULL
DROP PROCEDURE ThrowError
GO

CREATE PROCEDURE ThrowError
AS
BEGIN
DECLARE @errorNo INT,
@errorDesc VARCHAR(1000),
@errorProc VARCHAR(100),
@errorLineNumber INT,
@errorDateTime DATETIME2(7)

SELECT @errorNo = ERROR_NUMBER(),
@errorDesc = ERROR_MESSAGE(),
@errorProc = ERROR_PROCEDURE(),
@errorLineNumber = ERROR_LINE(),
@errorDateTime = GETDATE()

DECLARE @id INT = XACT_STATE()
IF( @errorNo < 50000)
BEGIN

IF XACT_STATE() > 0 ROLLBACK

INSERT INTO DataError
SELECT @errorNo,
@errorDesc,
@errorProc,
@errorLineNumber,
@errorDateTime

--RAISERROR(@errorDesc,16,1);
--RETURN 1

END
ELSE
BEGIN
RAISERROR(@errorDesc,16,1);
END
END;
GO

IF OBJECT_ID('test_child') IS NOT NULL
DROP PROCEDURE test_child
GO

CREATE PROCEDURE test_child
( @id INT,
@name VARCHAR(50)
)
AS
BEGIN
BEGIN TRY

INSERT INTO test_parent_table
SELECT @id,
@name

SELECT 1/0

RETURN 0
END TRY
BEGIN CATCH
EXEC ThrowError
RETURN 1
END CATCH
END
GO

IF OBJECT_ID('test_parent') IS NOT NULL
DROP PROCEDURE test_parent
GO
CREATE PROCEDURE test_parent
( @id INT,
@name VARCHAR(50)
)
AS
BEGIN
BEGIN TRY
BEGIN TRAN

INSERT INTO test_parent_table
VALUES( 1,'Harry')

EXEC test_child 2,'Kristena'

SELECT 1/0

INSERT INTO test_parent_table
VALUES( 3,'Chris')

IF( @@TRANCOUNT > 0)
COMMIT

RETURN 0
END TRY
BEGIN CATCH
EXEC ThrowError
END CATCH
END
GO


/* Test cases

EXEC test_parent 0,'test'

SELECT * FROM test_parent_table
SELECT * FROM DataError



*/

WHEn I run the test case above , the output in DataError is given below:-

ErrorDesc
1 Row :- Divide by zero error encountered.
2 Row :- Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 1, current count = 0.

Here second row is not supposed to come, this is sucking. Please help!


Regards
Ashok
ashok84.kr
ashok84.kr
Right there with Babe
Right there with Babe (780 reputation)Right there with Babe (780 reputation)Right there with Babe (780 reputation)Right there with Babe (780 reputation)Right there with Babe (780 reputation)Right there with Babe (780 reputation)Right there with Babe (780 reputation)Right there with Babe (780 reputation)

Group: General Forum Members
Points: 780 Visits: 677
AND ANSWER IS

IF OBJECT_ID('test_parent_table') IS NOT NULL
DROP TABLE test_parent_table
CREATE TABLE test_parent_table
( id INT,
name VARCHAR(30)
)
GO

IF OBJECT_ID('DataError') IS NOT NULL
DROP TABLE DataError
CREATE TABLE DataError
( ErrorNo INT,
ErrorDesc VARCHAR(1000),
ErrorProc VARCHAR(100),
ErrorLineNumber INT,
ErrorDateTime DATETIME2(7)
)
GO

IF OBJECT_ID('ThrowError') IS NOT NULL
DROP PROCEDURE ThrowError
GO

CREATE PROCEDURE ThrowError
AS
BEGIN
DECLARE @errorNo INT,
@errorDesc VARCHAR(1000),
@errorProc VARCHAR(100),
@errorLineNumber INT,
@errorDateTime DATETIME2(7)

SELECT @errorNo = ERROR_NUMBER(),
@errorDesc = ERROR_MESSAGE(),
@errorProc = ERROR_PROCEDURE(),
@errorLineNumber = ERROR_LINE(),
@errorDateTime = GETDATE()

DECLARE @id INT = XACT_STATE()
IF( @errorNo < 50000)
BEGIN

IF XACT_STATE() <> 0
BEGIN
ROLLBACK
DECLARE @id34 INT = XACT_STATE()
END

INSERT INTO DataError
SELECT @errorNo,
@errorDesc,
@errorProc,
@errorLineNumber,
@errorDateTime

RAISERROR(@errorDesc,16,1);
--RETURN 1

END
ELSE
BEGIN
RAISERROR(@errorDesc,16,1);
END
END;
GO

IF OBJECT_ID('test_child') IS NOT NULL
DROP PROCEDURE test_child
GO

CREATE PROCEDURE test_child
( @id INT,
@name VARCHAR(50)
)
AS
BEGIN
BEGIN TRY
BEGIN TRAN

INSERT INTO test_parent_table
SELECT @id,
@name

SELECT 1/0

DECLARE @id11 INT = @@TRANCOUNT
IF( @@TRANCOUNT > 0)
COMMIT

RETURN 0
END TRY
BEGIN CATCH
DECLARE @id1 INT = XACT_STATE()
DECLARE @id12 INT = @@TRANCOUNT
IF XACT_STATE() <> 0 ROLLBACK
EXEC ThrowError
END CATCH
END
GO

IF OBJECT_ID('test_parent') IS NOT NULL
DROP PROCEDURE test_parent
GO

/* Test cases

EXEC test_parent 0,'test'

SELECT * FROM test_parent_table
SELECT * FROM DataError



*/

CREATE PROCEDURE test_parent
( @id INT,
@name VARCHAR(50)
)
AS
BEGIN
BEGIN TRY
BEGIN TRAN

INSERT INTO test_parent_table
VALUES( 1,'Harry')

EXEC test_child 2,'Kristena'

SELECT 1/0

INSERT INTO test_parent_table
VALUES( 3,'Chris')

DECLARE @id11 INT = @@TRANCOUNT
IF( @@TRANCOUNT > 0)
COMMIT

RETURN 0
END TRY
BEGIN CATCH
DECLARE @id1 INT = XACT_STATE()
DECLARE @id12 INT = @@TRANCOUNT
IF XACT_STATE() <> 0 ROLLBACK
EXEC ThrowError
END CATCH
END
GO

/* Test cases

EXEC test_parent 0,'test'

SELECT * FROM test_parent_table
SELECT * FROM DataError



*/

It is now perfect : I am sorry to bother you guys..:-PHehe
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