Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

table variable vs temp table

declare  @i int = 1
if (@i= 2)
Begin 
    declare @t table (value int)
    insert into @t 
    select 1
    select * from @t
end
else 
    select *from @t  
---------------
declare  @i int = 1
if (@i= 2)
Begin 
    create table #t(value int)
    insert into #t
    select 1
end
else 
    select *from #t 

Why table variable is not getting invalid object name in this?

like image 332
omkar Avatar asked Jul 26 '26 05:07

omkar


1 Answers

The scope of a variable in T-SQL is not confined to a block. The scope of a local variable is the batch in which it is declared.

There was a request to make it possible to declare variables that are only visible within a block but Microsoft denied it. Here is the link

like image 182
Noor A Shuvo Avatar answered Jul 28 '26 03:07

Noor A Shuvo