MS SQL Tips & Tricks

How to test table existance

If You want to create table unless it already exists You just need to check then INFORMATION_SCHEMA.TABLES view. For example:

IF db_name()<>'master' and
   not exists (select * from INFORMATION_SCHEMA.TABLES
          where table_name='Dict' and table_type='BASE TABLE')
BEGIN

create table Dict
(
	Id bigint Identity(1,1) not null,
	Name varchar(32) not null default '',
	Item varchar(32) not null default ''
)

end;

Learn more tricks

Add Comment

Comments