Friday, January 15, 2010

Declaring variable inside 'IF' statement

If you declare some variable inside IF statement and the code
inside the IF statement is not going to be executed, can you use
the declared variable outside the IF statement?(after the IF statement).

Well, you can. The value of this variable will be NULL as without
assigning a value.

DECLARE @Flag bit

IF @Flag=1
BEGIN
    DECLARE @Str varchar(20)
    SET @Str='TestVariable'
END

SELECT @Str as [str]
------------------------------------------------------
NULL

No comments:

Post a Comment