[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 332
  • Last Modified:

How can I see if a table exists in SQL before creating it

Hello guys

I need to create a table in my database, but before creating it, I need to see if there is already this table, how can I do this?

thanks
0
hidrau
Asked:
hidrau
  • 3
  • 2
  • 2
2 Solutions
 
ralmadaCommented:

IF OBJECT_ID ('yourtable') IS NULL
 create TABLE yourtable ( ....

Open in new window

0
 
ralmadaCommented:
or more appropriately
IF OBJECT_ID (N'yourtable', N'U') IS NULL
      create table (....

Open in new window

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
YZlatCommented:
IF EXISTS (SELECT 1
    FROM INFORMATION_SCHEMA.TABLES
    WHERE TABLE_TYPE='BASE TABLE'
    AND TABLE_NAME='tablename')
        SELECT 'tablename exists.'
ELSE
        --CREATE YOUR TABLE HERE
0
 
ralmadaCommented:
hmmm, I've posted first. what's wrong with my suggestion? Can you please advise?
0
 
hidrauAuthor Commented:
I can't believe, I didn't split the point.

I need to reopen the question
0
 
hidrauAuthor Commented:
thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now