Solved

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

Posted on 2010-08-26
8
320 Views
Last Modified: 2012-05-10
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
Comment
Question by:hidrau
  • 3
  • 2
  • 2
8 Comments
 
LVL 41

Accepted Solution

by:
ralmada earned 250 total points
ID: 33532537

IF OBJECT_ID ('yourtable') IS NULL

 create TABLE yourtable ( ....

Open in new window

0
 
LVL 35

Assisted Solution

by:YZlat
YZlat earned 250 total points
ID: 33532546
0
 
LVL 41

Expert Comment

by:ralmada
ID: 33532554
or more appropriately
IF OBJECT_ID (N'yourtable', N'U') IS NULL

      create table (....

Open in new window

0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 35

Expert Comment

by:YZlat
ID: 33532573
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
 
LVL 41

Expert Comment

by:ralmada
ID: 33534773
hmmm, I've posted first. what's wrong with my suggestion? Can you please advise?
0
 
LVL 1

Author Comment

by:hidrau
ID: 33534825
I can't believe, I didn't split the point.

I need to reopen the question
0
 
LVL 1

Author Closing Comment

by:hidrau
ID: 33536672
thanks
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Suggested Solutions

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now