DELETE TOP command in SQL Server
The DELETE TOP command in SQL Server is used to delete records from a table in SQL and limits the number of records based on an existing value or percentage.
DELETE TOP command syntax
DELETETOP (giatri_dau) [PERCENT]
FROM bang
[WHERE dieu
_kien];
Variable name or variable value
state
Table wants to delete records from there.
WHERE dieu_kien
Option. The condition that the record must meet to be deleted.
TOP (giatri_dau)
This command will delete the first row in the result set based on giatri_dau. For example, TOP (10) will delete the top 10 that meet the criteria.
PERCENT
Option. If specified, the first rows are based on the percentage of giatri_dau of the result set. For example, TOP (10) PERCENT will insert 10% of the first value in the result set.
Note
There is no need to list all fields in the DELETE statement because it will delete all rows in the table.
For example - use the keyword TOP
DELETE TOP(10)
FROM nhanvien
WHERE ho
= 'Anderson';
In this example, the DELETE TOP command will delete the first 10 records in the table with the employee family, Anderson. If there are other records that also have the family name of Anderson, they will not be deleted.
For example - use the keyword TOP PERCENT
DELETE TOP (25) PERCENT
FROM nhanvien
WHERE ten = 'Sarah';
In this example, 25% of the DELETE TOP response's delete records will be deleted. The remaining 75% will not be deleted.
Previous article: SELECT INTO command in SQL Server
The following article: UNION operator in SQL Server
You should read it
- VIEW in SQL Server
- Foreign Key with Cascade Delete in SQL Server
- Delete the foreign key in SQL Server
- The TRUNCATE TABLE command in SQL Server
- The difference between Truncate and Delete in Microsoft SQL Server
- How to delete the database in MS SQL Server
- DROP USER command in SQL Server
- FUNCTION (Function) in SQL Server
May be interested
- CASE function in SQL Server (part 1)in sql server, the case function verifies the value based on the list of given conditions, then returns one or more results. in this article we will illustrate a number of different uses of this function in different situations.
- The sfc command in Windows(applies to windows server (semi-annual channel), windows server 2016, windows server 2012 r2, windows server 2012)
- The cacls command in Windowsthe cacls command displays or modifies an arbitrary access control list (dacl) on the specified file. the command applies to windows server (semi-annual channel), windows server 2016, windows server 2012 r2, windows server 2012.
- CASE statement in SQL Serverthis article will show you in detail how to use the case statement handling function in sql server with specific syntax and examples to better visualize and capture functions.
- ALTER LOGIN command in SQL Serverthe alter login command is used to modify the identity used to connect to sql server.
- SELECT TOP command in SQL Serverin sql server, the select top command is used to retrieve records from one or more tables in sql server and limit the number of return records based on a fixed value or percentage.
- SELECT INTO command in SQL Serverthe article guides using select into command in sql server with syntax and examples.
- Foreign Key with Cascade Delete in SQL Serverthis article explains how to use foreign key with cascade delete in sql server with syntax and examples.
- Prncnfg command in Windows(applies to windows server (semi-annual channel), windows server 2016, windows server 2012 r2, windows server 2012
- The route_ws2008 command in Windows(applies to windows server (semi-annual channel), windows server 2016, windows server 2012 r2, windows server 2012)