Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Declare error message before CATCH? Expand / Collapse
Author
Message
Posted Friday, January 18, 2013 3:46 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 8:40 AM
Points: 2,692, Visits: 3,383
In doing code review, I suggested to a developer to move their error variables that were used in the CATCH to the inside of the CATCH block in a stored proc. However, I am questioning myself now about this... Is this simply my preference? The developer mentioned that since this is a declarative language, the space had to be reserved no matter where it was. That seems correct, but I don't know how to test. Thoughts?

Thanks,

Jared
SQL Know-It-All

How to post data/code on a forum to get the best help - Jeff Moden
Post #1409126
Posted Friday, January 18, 2013 9:48 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:23 PM
Points: 13,272, Visits: 12,103
I would say it is mostly preference but I would agree with you. Why have all your variable declarations at the top? This isn't Pascal or Delphi. It makes sense to have everything together as much as possible. Why create spaghetti when you don't have to?

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1409159
Posted Sunday, January 20, 2013 8:17 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 7:08 PM
Points: 7,097, Visits: 12,599
SQLKnowItAll (1/18/2013)
The developer mentioned that since this is a declarative language, the space had to be reserved no matter where it was. That seems correct, but I don't know how to test. Thoughts?

I would challenge the notion that because SQL is a declarative language that a T-SQL procedure follows the idea of all variables in a batch being reserved up front. If that were the case then why is that I cannot use a variable in a batch prior to when it is declared? I am thinking that while DML and DDL queries are most definitely declarative the rest is procedural, i.e. executed as lines in the batch in the order they are submitted. That said, I know what you mean, how to test it?

My preference is aligned with both yours and Sean's. No reason to declare everything up front. I do end up moving my error variable declarations to the top of my procs sometimes though, depending on whether I have a need to do nested TRY/CATCH blocks.


__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Post #1409280
Posted Monday, January 21, 2013 5:09 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 3:12 AM
Points: 870, Visits: 2,780
SQLKnowItAll (1/18/2013)
..., but I don't know how to test.


Did someone steal your login credentials?

CREATE PROCEDURE TestErr
AS
DECLARE @Err NVARCHAR(255);

BEGIN TRY
SELECT 1 / 0
END TRY
BEGIN CATCH
SET @Err = 'Errrr';
RAISERROR(@Err, 16, -1);
END CATCH;
GO

EXEC TestErr;
GO

CREATE PROCEDURE TestErr2
AS
BEGIN TRY
DECLARE @Err NVARCHAR(255);
SELECT 1 / 0
END TRY
BEGIN CATCH
SET @Err = 'Err 2';
RAISERROR(@Err, 16, -1);
END CATCH;
GO

EXEC TestErr2;
GO





The SQL Guy @ blogspot

@SeanPearceSQL

About Me
Post #1409494
Posted Monday, January 21, 2013 6:59 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 8:40 AM
Points: 2,692, Visits: 3,383
Sean Pearce (1/21/2013)
SQLKnowItAll (1/18/2013)
..., but I don't know how to test.


Did someone steal your login credentials?

CREATE PROCEDURE TestErr
AS
DECLARE @Err NVARCHAR(255);

BEGIN TRY
SELECT 1 / 0
END TRY
BEGIN CATCH
SET @Err = 'Errrr';
RAISERROR(@Err, 16, -1);
END CATCH;
GO

EXEC TestErr;
GO

CREATE PROCEDURE TestErr2
AS
BEGIN TRY
DECLARE @Err NVARCHAR(255);
SELECT 1 / 0
END TRY
BEGIN CATCH
SET @Err = 'Err 2';
RAISERROR(@Err, 16, -1);
END CATCH;
GO

EXEC TestErr2;
GO


Nope, just that you misunderstood what I wanted to test. I know that the variable is in scope in these cases. However, what I was curious about was if it actually reserves the space for that variable when the proc runs, or only when it gets to the declare. So, if the DECLARE is in the CATCH block, does the space get reserved for that on execution or only if the code goes into that block.


Thanks,

Jared
SQL Know-It-All

How to post data/code on a forum to get the best help - Jeff Moden
Post #1409532
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse