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


Global Temp Tables: Triggers Vs Stored Procedure


Global Temp Tables: Triggers Vs Stored Procedure

Author
Message
naveenreddy.84
naveenreddy.84
Valued Member
Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)

Group: General Forum Members
Points: 56 Visits: 287
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,
John Rowan
John Rowan
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4072 Visits: 4510
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 - by Jeff Moden
GSquared
GSquared
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14371 Visits: 9729
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
Paul White
Paul White
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: 10338 Visits: 11350
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



Paul White
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
naveenreddy.84
naveenreddy.84
Valued Member
Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)

Group: General Forum Members
Points: 56 Visits: 287
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!!!
Paul White
Paul White
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: 10338 Visits: 11350
If by 'helping you out' you mean 'write it for you' then...no - not me :-P
(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



Paul White
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
GSquared
GSquared
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14371 Visits: 9729
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
John Rowan
John Rowan
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4072 Visits: 4510
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 - by Jeff Moden
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