SQL Journey

Journey of SQL Server & Microsoft Business Intelligence

Check existence of Table or Database before creating it in SQL Server

Posted by Prashant on June 9, 2010


To check if a table is exists or not a database:

--Check if table exists
IF EXISTS (SELECT 1 FROM sys.tables WHERE name = 'TableName')
PRINT 'TableName available.'
ELSE
PRINT 'TableName not available.'

Similar way we can also check if a database is exists or not before creating.

--Check if database exists
IF EXISTS (SELECT 1 FROM sys.databases WHERE name = 'DBName')
PRINT 'DBName available.'
ELSE
PRINT 'DBName not available.'

Please leave your valuable comments on this.

One Response to “Check existence of Table or Database before creating it in SQL Server”

  1. inspired said

    inspired…

    […]Check existence of Table or Database before creating it in SQL Server « SQL Journey[…]…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: