CREATE TABLE IF NOT EXISTS equivalent in SQL Server

Sourabh picture Sourabh · Jun 29, 2011 · Viewed 450.8k times · Source

CREATE TABLE IF NOT EXISTS works on mysql but fails with SQL Server 2008 R2. What is the equivalent syntax?

Answer

Neil Knight picture Neil Knight · Jun 29, 2011
if not exists (select * from sysobjects where name='cars' and xtype='U')
    create table cars (
        Name varchar(64) not null
    )
go

The above will create a table called cars if the table does not already exist.