Two alternatives:
1) put the DROP TABLE and CREATE TABLE in separate batches by
putting a "go" before the CREATE TABLE:
use tempdb
go
IF OBJECT_ID('t1') IS NOT NULL
BEGIN
DROP table t1
IF OBJECT_ID('t1') IS NOT NULL
PRINT '<<< FAILED DROPPING table tempdb..t1 >>>'
ELSE
PRINT '<<< DROPPED table tempdb..t1 >>>'
END
go
CREATE TABLE t1
(
c1 varchar(15) NULL
)
go
2) put the CREATE TABLE within the context of an EXECUTE IMMEDIATE
statement, so that the CREATE
isn't evaluated until the EXECUTE is executed, at which point the
older table with the same name will not exist:
use tempdb
go
IF OBJECT_ID('t1') IS NOT NULL
BEGIN
DROP table t1
IF OBJECT_ID('t1') IS NOT NULL
PRINT '<<< FAILED DROPPING table tempdb..t1 >>>'
ELSE
PRINT '<<< DROPPED table tempdb..t1 >>>'
END
execute
("
CREATE TABLE t1
(
c1 varchar(15) NULL
)
")
go