DECLARE @ToCreate bit
IF @ToCreate = 1
DECLARE @Table TABLE
(id int, name varchar(50) )
ELSE
INSERT INTO @Table (id, name)
select 1, 'a'
SELECT * FROM @Table
Variables, including table variables, are initialised during parsing time, not run time. Run-time flow control logic does not apply to DECLARE statements. @Table is created during the batch compilation and gets populated in run-time, as per flow control logic (@ToCreate is NULL, ELSE part is executed).
No comments:
Post a Comment