this is not a bug, it's supposed to work this way
the batch (everything before the "go") is compiled before being executed
if the table already exists, the create table statement will fail.
it works with exec immediate, because the compilation of the executed statement is delayed till actual execution time. that part is never reached if the table exists.
few approaches possible:
1. if you always want to drop & recreate all tables, then always perform a drop table before the create table (this means all data is lost every time you rerun the script)
2. use exec immediate, like the way you do now
3. if you have 1 script per table, you could select syb_quit() if the table already exists
e.g.
IF EXISTS | (SELECT * FROM sysobjects WHERE name="TestExec") |
select syb_quit()
go
create table....
go