Wednesday, April 1, 2015

How to Check and Delete if a Table or Database already exists in SQL Server

SQL Query to Check and Delete if a Table or Database already exists in SQL Server
'Checking the Table Existence , If yes Dropping it.
'Here it checks the Table Name from all the Schemas.
IF EXISTS ( SELECT NAME from SYS.TABLES where NAME ='EMPLOYEE')
DROP TABLE EMPLOYEE
GO

'Creating the Table again after Dropping.
Create Table EMPLOYEE (
EID INT,
EMPNAME VARCHAR(25)
DEPT VARCHAR(25),
DOJ DATE
SAL DECIMAL (10,2)
)
GO 

'Checking the Database Existence , If yes Dropping it.

IF EXISTS ( SELECT [Name] From SYS.DATABASES Where [Name] = 'TAMATAM' ) 
DROP DATABASE TAMATAM
GO 

'Creating the Database again after Dropping.
CREATE DATABASE TAMATAM
GO

Notes:
Use caution when dropping tables. Once you drop a table, you will not be able to get it back unless you restore a backup or defined in a Transaction with Save Point to Rollback.

--------------------------------------------------------------------------------------------------------
Thanks, TAMATAM ; Business Intelligence & Analytics Professional
--------------------------------------------------------------------------------------------------------

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.

Featured Post from this Blog

How to compare Current Snapshot Data with Previous Snapshot in Power BI

How to Dynamically compare two Snapshots Data in Power BI Scenario: Suppose, we have a sample Sales data, which is stored with Monthly Snaps...

Popular Posts from this Blog