﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / SQL Server 2008 - General  / Declare error message before CATCH? / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Tue, 21 May 2013 13:24:09 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Declare error message before CATCH?</title><link>http://www.sqlservercentral.com/Forums/Topic1409126-391-1.aspx</link><description>[quote][b]Sean Pearce (1/21/2013)[/b][hr][quote][b]SQLKnowItAll (1/18/2013)[/b][hr]..., but I don't know how to test. [/quote]Did someone steal your login credentials?[code="sql"]CREATE PROCEDURE TestErrASDECLARE @Err NVARCHAR(255);	BEGIN TRY		SELECT 1 / 0	END TRY	BEGIN CATCH		SET @Err = 'Errrr';		RAISERROR(@Err, 16, -1);	END CATCH;GOEXEC TestErr;GOCREATE PROCEDURE TestErr2AS	BEGIN TRY		DECLARE @Err NVARCHAR(255);		SELECT 1 / 0	END TRY	BEGIN CATCH		SET @Err = 'Err 2';		RAISERROR(@Err, 16, -1);	END CATCH;GOEXEC TestErr2;GO[/code][/quote]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.</description><pubDate>Mon, 21 Jan 2013 06:59:44 GMT</pubDate><dc:creator>SQLKnowItAll</dc:creator></item><item><title>RE: Declare error message before CATCH?</title><link>http://www.sqlservercentral.com/Forums/Topic1409126-391-1.aspx</link><description>[quote][b]SQLKnowItAll (1/18/2013)[/b][hr]..., but I don't know how to test. [/quote]Did someone steal your login credentials?[code="sql"]CREATE PROCEDURE TestErrASDECLARE @Err NVARCHAR(255);	BEGIN TRY		SELECT 1 / 0	END TRY	BEGIN CATCH		SET @Err = 'Errrr';		RAISERROR(@Err, 16, -1);	END CATCH;GOEXEC TestErr;GOCREATE PROCEDURE TestErr2AS	BEGIN TRY		DECLARE @Err NVARCHAR(255);		SELECT 1 / 0	END TRY	BEGIN CATCH		SET @Err = 'Err 2';		RAISERROR(@Err, 16, -1);	END CATCH;GOEXEC TestErr2;GO[/code]</description><pubDate>Mon, 21 Jan 2013 05:09:13 GMT</pubDate><dc:creator>Sean Pearce</dc:creator></item><item><title>RE: Declare error message before CATCH?</title><link>http://www.sqlservercentral.com/Forums/Topic1409126-391-1.aspx</link><description>[quote][b]SQLKnowItAll (1/18/2013)[/b][hr]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?[/quote]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.</description><pubDate>Sun, 20 Jan 2013 08:17:12 GMT</pubDate><dc:creator>opc.three</dc:creator></item><item><title>RE: Declare error message before CATCH?</title><link>http://www.sqlservercentral.com/Forums/Topic1409126-391-1.aspx</link><description>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?</description><pubDate>Fri, 18 Jan 2013 21:48:23 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>Declare error message before CATCH?</title><link>http://www.sqlservercentral.com/Forums/Topic1409126-391-1.aspx</link><description>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?</description><pubDate>Fri, 18 Jan 2013 15:46:27 GMT</pubDate><dc:creator>SQLKnowItAll</dc:creator></item></channel></rss>