i've got strange behavior below code.
--important: not use 'go' since terminate --the batch , usable in microsoft tools --and not in code itself. --i don't need workaround want know why --this behavior happens. --create first time if doesn't exists if object_id('tempdb.dbo.#temp') null begin create table #temp (id datetime) end --i've created should evaluates false if object_id('tempdb.dbo.#temp') null begin print 'does not exists' --uncomment below line , see --an error saying table exists --even though if evaluate true --create table #temp (id datetime) end else begin print 'exists' end
i'm trying achieve more complex script end problem verify if temporary table exists , create if necessary.
in part of code can have or no temporary table created. check if exists , if doesn't exists want create it.
the problem if print message evaluates exists
if uncomment part does not exists
, create new 1 avoided run because exists.
why uncommenting create table #temp (id datetime)
make sql run true
part of if
statement if evaluates false
?
i'm running sql server 2008 (10.50.2500) in sql management studio 11.0.2100.60
try way:
if object_id('#temp') not null begin exec('drop table #temp ') end go create table tempdb..#temp (id datetime) if object_id('#temp') null begin select 'does not exists' end else begin select 'exists' end
or
if object_id('tempdb.dbo.#temp') null begin exec('create table #temp (id datetime)') end --i've created should evaluates false if object_id('tempdb.dbo.#temp') null begin print 'does not exists' --uncomment below line , see --an error saying table exists --even though if evaluate true --create table #temp (id datetime) end else begin print 'exists' end
Comments
Post a Comment