Sunday, February 10, 2013

How to use Delete Statement in SQL Server

SQL Server DELETE Command
The DELETE is DML Command which is used to delete a specific Record based on Where Clause with condition ; or all the records from the Table, if no Where condition is specified.
As DELETE is a DML Command it will not delete/impact the Structure of a Table.
Syntax:
DELETE FROM Table_name WHERE some_column=some_value

Note
Notice the WHERE clause in the DELETE syntax. The WHERE clause specifies which record or records that should be deleted. If you omit the WHERE clause, all records will be deleted!

Example:
Suppose we have a EMP Table as follows :
Emp_ID
E_Name
Job
Dept_ID
Salary
1001
Ravi Teja Reddy
Analyst
1
20000
1002
Karun
Team Lead
2
35000
1003
Ravi Kumar Rao
Analyst
1
10000
1004
Tamatam
Team Lead
3
25000
1005
Sai Kumar Teja
Analyst
2
15000
1006
William
Manager
3
50000
1007
Vikram Teja Reddy
Analyst
2
180000
1008
Varun
Team Lead
1
33000
1006
Smith
Team Lead
3
50000

Criteria -I : Deleting a Particular Record :
Delete from EMP WHERE E_Name='Tamatam'
Result :
Emp_ID
E_Name
Job
Dept_ID
Salary
1001
Ravi Teja Reddy
Analyst
1
20000
1002
Karun
Team Lead
2
35000
1003
Ravi Kumar Rao
Analyst
1
10000
1005
Sai Kumar Teja
Analyst
2
15000
1006
William
Manager
3
50000
1007
Vikram Teja Reddy
Analyst
2
180000
1008
Varun
Team Lead
1
33000
1006
Smith
Team Lead
3
50000

Criteria -II : Deleting a Multiples Records :
Delete from EMP WHERE Emp_ID IN (1001,1003,1005,1007)
Result :
Emp_ID
E_Name
Job
Dept_ID
Salary
1002
Karun
Team Lead
2
35000
1004
Tamatam
Team Lead
3
25000
1006
William
Manager
3
50000
1008
Varun
Team Lead
1
33000
1009
Smith
Team Lead
3
50000

Criteria -III :To Delete all Records :
It is possible to delete all rows in a table without deleting the Table. This means that the table structure, attributes, and indexes will be remain , only Records will be deleted.
Delete* from EMP
Alert: 
Be very careful when deleting records. You cannot undo this statement,unless you create a Transaction with save point.

--------------------------------------------------------------------------------------------------------

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