another T-SQL gotcha

One funny (or annoying) thing about T-SQL is that it does not allow you to declare variables (or create temp tables) with the same name in mutually exclusive units of code.  Here's an example of what I mean:

DECLARE @test bit
SET @test = 0

IF (@test = 0)
  BEGIN
    DECLARE @monkey int
  END
ELSE
  BEGIN
    DECLARE @monkey int
  END

If you run that in Query Analyzer you'll get the following message:

Server: Msg 134, Level 15, State 1, Line 11
The variable name '@monkey' has already been declared. Variable names must be unique within a query batch or stored procedure.

 

Print | posted on Monday, February 26, 2007 11:33 PM

Comments on this post

No comments posted yet.

Your comment:

 (will show your gravatar)
 
Please add 2 and 4 and type the answer here: