Declare error message before CATCH?

  • 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?

    Jared
    CE - Microsoft

  • 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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • 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

  • 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[/url]

    @SeanPearceSQL

    About Me[/url]

  • 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.

    Jared
    CE - Microsoft

Viewing 5 posts - 1 through 4 (of 4 total)

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