Instagram
youtube
Facebook
Twitter

MS SQL Server- Delete Data

MS SQL Server DELETE Data

  • Open the SSMS.
  • In Object Explorer, Expand the database folder and then the table folder.
  • Select the table where you want to make changes and right-click on it. In the pop-up menu, select Script Table as then Delete to and then New Query Editor Window options from the list.
  • On clicking the New Query Editor Window, we will reach the following query page. Use the Query to update the table:
DELETE FROM [database_name].[dbo].[table_name][WHERE condition];
  • Example:
DELETE FROM [University].[dbo].[STUDENT] WHERE ID ='EN19CS301254';

SQL Server DELETE Top Statement

  • TOP is used with a DELETE statement to delete the top rows of a table.
  • Syntax to use TOP with delete:
DELETE TOP (top_value) [ PERCENT ]  FROM [database_name].[dbo].[table_name][WHERE conditions];  
  • The table specifies a table from where we want to delete records.
  • WHERE condition is optional . If conditions are fulfilled, then records will be deleted.
  • TOP(top_value) is used to delete the top number of rows. For Example, TOP(10) would delete the top 10 rows.
  • PERCENT is optional used to delete the percentage of top rows.
  • Example using TOP(top_value):
DELETE TOP(2) FROM [University].[dbo].[student] WHERE CGPA=8.0;
  • Example using TOP with PERCENT
DELETE TOP(25)PERCENT FROM [University].[dbo].[student] WHERE CGPA>=8.0;