Can anyone explain why this works:
use MyDb1
if NOT EXISTS (select * from sys.objects where name = 'MyTable' and type = 'U' )
create table MyTable(MyColumn int not null)
use MyDb2
if NOT EXISTS (select * from sys.objects where name = 'MyTable' and type = 'U' )
create table MyTable(MyColumn int not null)
But this is not so:
use MyDb1
mylabel:
if NOT EXISTS (select * from sys.objects where name = 'MyTable' and type = 'U' )
create table MyTable(MyColumn int not null)
if(DB_NAME()='MyDb1')
begin
use MyDb2
goto mylabel
end
The flow is correct and the second check for IF NOT EXISTS works, but when it tries to create a table, I get the following error.
The database already has an object named MyTable.
this is greatly simplified, but at the same time it will save a lot of duplicate table creations in two almost identical databases.
source
share