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


Declare error message before CATCH?


Declare error message before CATCH?

Author
Message
SQLKnowItAll
SQLKnowItAll
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2798 Visits: 3681
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
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16566 Visits: 17016
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)
Orlando Colamatteo
Orlando Colamatteo
SSCrazy Eights
SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)

Group: General Forum Members
Points: 8239 Visits: 14368
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
Sean Pearce
Sean Pearce
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1170 Visits: 3432
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
SQLKnowItAll
SQLKnowItAll
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2798 Visits: 3681
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
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