Solved

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

Posted on 2010-08-26
8
324 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MS SQL page split per second is high 19 104
Delete from table 6 47
Selection from table2 where criteria for table1 10 49
What is this datetime? 1 19
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

860 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