Global Temp Tables: Triggers Vs Stored Procedure

  • Hi All,

    I am trying to access global temp table

    1) Between stored procedure in a nested format(Stored Procedure Proc1 calling another stored procedure Proc2) and

    2) between stored procedure called from a trigger

    My Code goes as below:

    Scenario 1) Here Proc1 calls another procedure Proc2. A global temp table ##ErrLogging is declared in Proc1 and is populated with the error message(if any) in the catch block of Proc2. The error message is visible even if the TRAN rollbacks in the catch block of Proc1.

    Since the global temp table is intact even after the rollback statement; i would like to use this to insert into physical log table in my database.

    CREATE PROCEDURE [dbo].[Proc1]

    AS

    BEGIN TRY

    BEGIN TRAN

    IF object_id('tempdb..##ErrLogging') is not null

    DROP TABLE ##ErrLogging

    CREATE TABLE ##ErrLogging(errMesg NVARCHAR(200))

    EXEC Proc2

    COMMIT TRAN

    END TRY

    BEGIN CATCH

    ROLLBACK TRAN

    SELECT * FROM ##ErrLogging

    END CATCH

    GO

    CREATE PROCEDURE [dbo].[Proc2]

    AS

    BEGIN TRY

    RAISERROR('My Error',16,1)

    END TRY

    BEGIN CATCH

    INSERT INTO ##ErrLogging VALUES('My Message');

    END CATCH

    Scenario: Trigger declare the global temp table and calls Proc2 as follows

    CREATE TRIGGER [tr_OnInsertMy_Test] ON [dbo].[My_Test]

    AFTER INSERT

    AS

    BEGIN TRY

    BEGIN TRAN

    IF object_id('tempdb..##ErrLogging') is not null

    DROP TABLE ##ErrLogging

    CREATE TABLE ##ErrLogging (errMesg NVARCHAR(200))

    EXEC Proc2

    COMMIT TRAN

    END TRY

    BEGIN CATCH

    ROLLBACK TRAN

    SELECT * FROM ##ErrLogging

    END CATCH

    Proc2 remain same as in scenario one

    But in this scenario; i faced with an error at the insert statement in the Proc2 mentioning "Invalid Object Name ##ErrLogging".

    Is this the expectable result?Is there something am doing wrong here?

    I need to access the global temp table in the catch block of Trigger in order to log error details into the physical table

    Regards,

  • Is this a single user application? If not, how do you plan to keep the global temp table name unique for the thread that is creating it?

    Using global temp tables for error handling is a bad idea. Why do you think you need to use a temp table to pass error information back to the calling SP?

    BOL has a bunch of detailed examples of using TRY/CATCH blocks to process errors. I suggest you read them. You're going to get yourself into a world of hurt trying to use global temp tables to do this.

    John Rowan

    ======================================================
    ======================================================
    Forum Etiquette: How to post data/code on a forum to get the best help[/url] - by Jeff Moden

  • I'll second the warning on using a global temp for this. The moment two processes or users call the proc at the same time, you've got a problem.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • I only looked quickly, I admit, but if you want to preserve log information over a transaction boundary, why don't you use a table variable instead?

    Table variables aren't affected by transactions.

    Paul

  • Hi,

    Can anyone help me out with writing the code(scenario 2) using temp variables for logging errors outside the transaction scope.

    Thanks for your help!!!

  • If by 'helping you out' you mean 'write it for you' then...no - not me 😛

    (I already have a job!)

    The best way to learn stuff is to take an idea and experiment with it. Create a table variable, start a transaction, insert some data into it, and then roll back your transaction. Finally, select everything from the table variable.

    That should give you an idea how to proceed. If you get stuck, post back with your best attempt so far, and why you are stuck.

    Paul

  • naveenreddy.84 (7/7/2009)


    Hi,

    Can anyone help me out with writing the code(scenario 2) using temp variables for logging errors outside the transaction scope.

    Thanks for your help!!!

    Actually, for what you're doing, you don't need either. You're creating a single-row, nvarchar(200) value. Why not just assign it to an output parameter in the second proc? Then you can have the first proc get the value from that, and you're good to go.

    Are you familiar with output parameters?

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Or, create a standard error trapping method so you can always capture an error created in a nested TRY/CATCH block.

    John Rowan

    ======================================================
    ======================================================
    Forum Etiquette: How to post data/code on a forum to get the best help[/url] - by Jeff Moden

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply