DELETE command in SQL Server

The DELETE statement in SQL Server (Transact-SQL) is used to 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 FRO M bang 
[WHERE dieu_kien];

The full syntax of the DELETE command is as follows.

 DELETE [TOP (g iatri_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 example, 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 example, 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 example - use 1 condition

 DELETE FROM nh anvien 
WHERE 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 has un (*) 
FROM nhanvien
WHERE ten = 'Sar ah';

For example - use 2 conditions

 DELETE FROM  nhanvien 
WHERE 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 coun t (*) 
FROM nhanvien
WHERE ho = 'Johnson'
AND nhanvien_id > = 80;

For example - use the keyword TOP

 DELETE  TOP(3) 
FROM nhanvien
WHERE 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 example - 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 example. 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 FROM nhanvien 
WHERE EXISTS
(SELECT *
FROM danhba
WHERE danhba.danhba_id = nhanvien.nhanvien_id
AND 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.

If you want to determine the number of deleted lines, run the SELECT command before deleting.

 SELECT count (*) 
FROM nhanvien
WHERE EXITS
(SELECT *
FROM danhba
WHERE danhba.danhba_id = nhanvien.nhanvien_id
AND danhba.da nhba_id <100);

Previous article: UPDATE command in SQL Server

Lesson: TRUNCATE TABLE command in SQL Server

5 ★ | 1 Vote

May be interested

  • CASE function in SQL Server (part 1)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 WindowsThe 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 in Windows
    the 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 ServerCASE statement in SQL Server
    this 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 ServerALTER LOGIN command in SQL Server
    the alter login command is used to modify the identity used to connect to sql server.
  • SELECT TOP command in SQL ServerSELECT TOP command in SQL Server
    in 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 ServerSELECT INTO command in SQL Server
    the article guides using select into command in sql server with syntax and examples.
  • Foreign Key with Cascade Delete in SQL ServerForeign Key with Cascade Delete in SQL Server
    this article explains how to use foreign key with cascade delete in sql server with syntax and examples.
  • Prncnfg command in WindowsPrncnfg 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 WindowsThe route_ws2008 command in Windows
    (applies to windows server (semi-annual channel), windows server 2016, windows server 2012 r2, windows server 2012)