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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With