SQL Server Equivalent to MySQL's CREATE TABLE IF NOT EXISTS
MySQL's CREATE TABLE IF NOT EXISTS syntax is used to create a table if it does not already exist. This syntax is not supported in SQL Server. However, there is a workaround using a combination of IF NOT EXISTS and SELECT FROM SYSOBJECTS to achieve the same functionality.
To create a table called "cars" if it does not already exist in SQL Server, use the following syntax:
if not exists (select * from sysobjects where name='cars' and xtype='U') create table cars ( Name varchar(64) not null ) go
The IF NOT EXISTS condition checks if the table "cars" already exists in the database by querying the sysobjects system table. If the table does not exist, the CREATE TABLE statement is executed. The xtype='U' condition ensures that only user tables are checked, excluding system tables.
The above is the detailed content of How to Create a SQL Server Table Only If It Doesn't Already Exist?. For more information, please follow other related articles on the PHP Chinese website!