Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Table variable based error logging inside Transactions


Table variable based error logging inside Transactions

Author
Message
Aneesh Soman
Aneesh Soman
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 104
Comments posted to this topic are about the item Table variable based error logging inside Transactions
Anatoliy-168606
Anatoliy-168606
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 Visits: 134
Is it really a necessity to use a table variable?

This is shorter with the same result:

CREATE TABLE AppleTest
( testint INT,
inserted_time DATETIME2
)
--Error logging table
CREATE TABLE Errorlog
( errordetails NVARCHAR(MAX),
loggedtime DATETIME2
)
--Starting the transaction
BEGIN TRAN
BEGIN
--Beginning the try block which has one correct line and then a divide by zero exception causing line
BEGIN TRY
INSERT INTO AppleTest
SELECT 5/5,GETDATE() --This is valid

INSERT INTO AppleTest
SELECT 1/0,GETDATE() --Error line as divide by zero will cause an error

INSERT INTO AppleTest
SELECT 10/2,GETDATE()-- Valid ;This is unreachable as control goes to catch after encountering the error

COMMIT TRAN -- This is unreachable as control goes to catch after encountering the error
END TRY
--Beginning the catch block which has insertion to a errolog TABLE VARIABLE and then ROLLBACK
BEGIN CATCH
ROLLBACK TRAN -- This will rollback the transaction and the valid insert Select 5/5,getdate() will now be rolled back.But the insertion in the TABLE VARIABLE will not be rolled back

INSERT INTO Errorlog
SELECT ERROR_MESSAGE(),GETDATE() --error_message() returns the error message,getdate() to store the time at which the error occured

END CATCH
END
--Now check the output in the physical error table
SELECT * FROM Errorlog
--Now check the output in the physical table
SELECT * FROM AppleTest
Aneesh Soman
Aneesh Soman
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 104
Thanks for the reply.

Table variables can be handy if we want to capture more info other the error message, like some record which got failed, the user who tried to insert that record . This was a pretty simple demo example. Consider a case where some user was doing some insert which contained this error, we can capture the record he was trying to insert, info about him, i mean more specific details about the error.
Iwas Bornready
Iwas Bornready
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10350 Visits: 885
Thanks for the script.
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