
Untitled
By: a guest on
Jul 4th, 2012 | syntax:
None | size: 1.13 KB | hits: 14 | expires: Never
GOTO label after USE database switch
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)
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
set nocount on
use tempdb
create database db1
create database db2
GO
use db1
mylabel:
print 'in-' + db_name()
if NOT EXISTS (select * from sys.objects where name = 'MyTable' and type = 'U' )
begin
print 'create-' + db_name()
create table MyTable(MyColumn int not null)
-- exec ('create table MyTable(MyColumn int not null)') -- <<<
end
if(DB_NAME()='db1')
begin
print 'switch'
use db2
goto mylabel
end
GO
use tempdb
drop database db1
drop database db2
in-db1
create-db1
switch
in-db2
create-db2
Msg 2714, Level 16, State 6, Line 9
There is already an object named 'MyTable' in the database.