about Grape Solutions
and solutions
references, partners
open positions
get to know us

T-SQL DECLARE is not an instruction

Dercsár Péter2009.12.23. 02:26

I've never thought about if it is or not, until I had to DECLARE things inside a loop in T-SQL. Maybe this not news for many, but it was for me.

It is evaluated at compile time. When your code (function or stored procedure) starts to execute, all DECLARE-d variables are ready for action.

An example:

IF (1=0) BEGIN
    DECLARE @some TABLE(
        number int)

    INSERT INTO @some VALUES (12)
END

INSERT INTO @some VALUES (23)

SELECT * FROM @some


If DECLARE was an instruction, it should fail at compile, since @some is not known to exist or not. But it's not the case.
@some is declared, but the value 12 is not inserted, since the IF condition is false. However, the second insert does.

That's all folks!

There are no comments.

Leave a comment!

Name: *
Url:
E-mail:
Tag cloud
audio (1) browser (1) div (1) html (1) join (1) linq (1) performance optimization (1) Pluto (1) Silverlight (2) svn (1) synthesizer (1) t-sql (1) union (1) workstation (1)