a Grape Solutions-ről
és megoldásaink
referenciák, partnerek
nyitott pozícióink
elérhetőségeink

T-SQL DECLARE is not an instruction

Dercsár Péter2009.12.23.

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!

Nincsenek hozzászólások.

Szólj hozzá!

Név: *
Url:
E-mail:
Címkefelhő
bdd (1) browser (1) egyéb (2) entity framework (1) linq (1) linq2sql (1) sales (1) test (1) welcome (1) windows (1)
Archívum
Dercsár Peti blogja:
Nincs bejegyzés