Table of Contents
MS SQL Server: DELETE Command in SQL Server is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.
The DELETE statement in SQL Server (Transact-SQL) helps delete one or more records from a table in SQL Server.
DELETE command syntax in SQL Server
Simple syntax of DELETE command is as follows.
DELETE FROM bang[WHERE dieu_kien];
The full syntax of the DELETE command is as follows.
DELETE [TOP (giatri_dau) [PERCENT] ]FROM bang[WHERE dieu_kien];
Variable name or variable value
state
Table needs to delete the record.
WHERE dieu_kien
Option. Conditions that the record must meet to be deleted.
TOP (giatri_dau)
Option. If specifically, it will insert the first value of the row based on giatri_dau. For instance,, TOP (10) will insert the first 10 rows from the result set.
PERCENT
Option. If specified, the first rows are based on the percentage of giatri_dau of the result set. For instance,, TOP (10) PERCENT will insert 10% of the first value in the result set.
Note
There is no need to list fields in the DELETE statement because you will delete all rows in the table.
For instance, - use 1 condition
DELETE FROM nhanvienWHERE ten = 'Sarah';
This command will delete all records in the table with the employee name Sarah.
If you want to check the number of deleted items, run the SELECT command before executing the delete command.
SELECT hasun (*)FROM nhanvienWHERE ten = 'Sarah';
For instance, - use 2 conditions
DELETE FROMnhanvienWHERE ho = 'Johnson'AND nhanvien_id >= 80;
This command will delete all records in the table if the staff name is Johnson and nhanvien_id is greater than or equal to 80.
To know the number of rows deleted, run the SELECT statement below before running the DELETE command.
SELECT count (*)FROM nhanvienWHERE ho = 'Johnson'AND nhanvien_id> = 80;
For instance, - use the keyword TOP
DELETETOP(3)FROM nhanvienWHERE ho ='Johnson';
This will delete the first 3 records in the table when the employee's family name is Johnson. If there are other records in this table that have a family name of Johnson, then they are not affected by this DELETE.
For instance, - use EXISTS clause
You can perform a more complex delete command, such as deleting records in a table based on values in another table, For instance,. Because it is impossible to render more than one table in the FROM clause in the DELETE statement, the EXISTS clause can be used as follows.
DELETE FROMnhanvienWHERE EXISTS(SELECT *FROM danhbaWHERE danhba.danhba_id = nhanvien.nhanvien_idAND danhba.danhba_id<100);
This DELETE statement will delete all records in the table when there is a record in the list of names that listener_id is less than 100 and list_id matches nhanvien_id.
Lesson: TRUNCATE TABLE command in SQL Server
FAQ
What should you know about dELETE command syntax in SQL Server?
Simple syntax of DELETE command is as follows.
What is MS SQL Server: DELETE Command in SQL Server?
The DELETE statement in SQL Server (Transact-SQL) helps delete one or more records from a table in SQL Server.
Why is MS SQL Server: DELETE Command in SQL Server important?
A clear understanding of MS SQL Server: DELETE Command in SQL Server helps you make informed decisions, avoid common mistakes, and use the relevant tools or techniques more effectively.
Reader Comments 0
Sign in with email or Google to join the discussion.