Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6096883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:58:43+00:00 2026-05-23T12:58:43+00:00

I want to verify if a table exists in a database, and if it

  • 0

I want to verify if a table exists in a database, and if it doesn’t exist, to create it.
How can I get a list of all the tables in the current database?

I could get the database list with a SELECT like this:

SELECT * FROM sys.databases

What’s left is to create the table if it doesn’t exist.

I also tried to create the tables at the same time with the database like this:

if not exists(select * from sys.databases where name = 'db')
begin 
    create database [db]
    use [db];
    create table [test] (
         Time datetime,
         Message varchar(1024) )
    end

But it gives me error on the ‘use’ line, saying that ‘db’ doesn’t exist. This time, I will try to do this in 2 different commands.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:58:44+00:00Added an answer on May 23, 2026 at 12:58 pm

    This should give you a list of all the tables in your database

    SELECT Distinct TABLE_NAME FROM information_schema.TABLES
    

    So you can use it similar to your database check.

    If NOT EXISTS(SELECT Distinct TABLE_NAME FROM information_schema.TABLES Where TABLE_NAME = 'Your_Table')
    BEGIN
        --CREATE TABLE Your_Table
    END
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

if i have a field in my table that i want to verify exists,
Originally, I just want to verify that session_start locks on session. So, I create
just want to verify that database partition is implemented only at the database level,
So here's the setup. I have two tables: CREATE TABLE dbo.TmpFeesToRules1(Name varchar, LookupId int)
So I made some tables programatically and I want to verify it's structure is
I made a small database(1 table) in phpMyAdmin. One of the fields I want
I want to verify if a computer is running .net 3.5 or 3.5 sp1,
I want to verify whether an e-mail is real or not. Some body told
I want to verify that my understanding is correct. This kind of thing is
I want to verify if I'm in the last cell of the UITable. Should

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.