Skip to content

Commit 3767721

Browse files
authored
delete, applies to, escapes, formatting examps.
1 parent 55844ab commit 3767721

1 file changed

Lines changed: 38 additions & 45 deletions

File tree

docs/t-sql/statements/delete-transact-sql.md

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "DELETE (Transact-SQL) | Microsoft Docs"
33
ms.custom: ""
4-
ms.date: "03/17/2017"
4+
ms.date: "05/10/2017"
55
ms.prod: "sql-non-specified"
66
ms.reviewer: ""
77
ms.suite: ""
@@ -86,7 +86,7 @@ DELETE FROM [database_name . [ schema ] . | schema. ] table_name
8686
```
8787

8888
## Arguments
89-
WITH <common_table_expression>
89+
WITH \<common_table_expression>
9090
Specifies the temporary named result set, also known as common table expression, defined within the scope of the DELETE statement. The result set is derived from a SELECT statement.
9191

9292
Common table expressions can also be used with the SELECT, INSERT, UPDATE, and CREATE VIEW statements. For more information, see [WITH common_table_expression &#40;Transact-SQL&#41;](../../t-sql/queries/with-common-table-expression-transact-sql.md).
@@ -101,9 +101,7 @@ DELETE FROM [database_name . [ schema ] . | schema. ] table_name
101101
The alias specified in the FROM *table_source* clause representing the table or view from which the rows are to be deleted.
102102

103103
*server_name*
104-
||
105-
|-|
106-
|**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] through [!INCLUDE[ssCurrent](../../includes/sscurrent-md.md)].|
104+
**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] through [!INCLUDE[ssCurrent](../../includes/sscurrent-md.md)].
107105

108106
The name of the server (using a linked server name or the [OPENDATASOURCE](../../t-sql/functions/opendatasource-transact-sql.md) function as the server name) on which the table or view is located. If *server_name* is specified, *database_name* and *schema_name* are required.
109107

@@ -121,20 +119,18 @@ DELETE FROM [database_name . [ schema ] . | schema. ] table_name
121119
The view referenced by *table_or_view_name* must be updatable and reference exactly one base table in the FROM clause of the view definition. For more information about updatable views, see [CREATE VIEW &#40;Transact-SQL&#41;](../../t-sql/statements/create-view-transact-sql.md).
122120

123121
*rowset_function_limited*
124-
||
125-
|-|
126-
|**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] through [!INCLUDE[ssCurrent](../../includes/sscurrent-md.md)].|
122+
**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] through [!INCLUDE[ssCurrent](../../includes/sscurrent-md.md)].
127123

128124
Either the [OPENQUERY](../../t-sql/functions/openquery-transact-sql.md) or [OPENROWSET](../../t-sql/functions/openrowset-transact-sql.md) function, subject to provider capabilities.
129125

130-
WITH **(** <table_hint_limited> [... *n*] **)**
126+
WITH **(** \<table_hint_limited> [... *n*] **)**
131127
Specifies one or more table hints that are allowed for a target table. The WITH keyword and the parentheses are required. NOLOCK and READUNCOMMITTED are not allowed. For more information about table hints, see [Table Hints &#40;Transact-SQL&#41;](../../t-sql/queries/hints-transact-sql-table.md).
132128

133-
<OUTPUT_Clause>
129+
\<OUTPUT_Clause>
134130
Returns deleted rows, or expressions based on them, as part of the DELETE operation. The OUTPUT clause is not supported in any DML statements targeting views or remote tables. For more information, see [OUTPUT Clause &#40;Transact-SQL&#41;](../../t-sql/queries/output-clause-transact-sql.md).
135131

136132
FROM *table_source*
137-
Specifies an additional FROM clause. This [!INCLUDE[tsql](../../includes/tsql-md.md)] extension to DELETE allows specifying data from <table_source> and deleting the corresponding rows from the table in the first FROM clause.
133+
Specifies an additional FROM clause. This [!INCLUDE[tsql](../../includes/tsql-md.md)] extension to DELETE allows specifying data from \<table_source> and deleting the corresponding rows from the table in the first FROM clause.
138134

139135
This extension, specifying a join, can be used instead of a subquery in the WHERE clause to identify rows to be removed.
140136

@@ -149,7 +145,7 @@ DELETE FROM [database_name . [ schema ] . | schema. ] table_name
149145

150146
- Positioned deletes use the CURRENT OF clause to specify a cursor. The delete operation occurs at the current position of the cursor. This can be more accurate than a searched DELETE statement that uses a WHERE *search_condition* clause to qualify the rows to be deleted. A searched DELETE statement deletes multiple rows if the search condition does not uniquely identify a single row.
151147

152-
<search_condition>
148+
\<search_condition>
153149
Specifies the restricting conditions for the rows to be deleted. There is no limit to the number of predicates that can be included in a search condition. For more information, see [Search Condition &#40;Transact-SQL&#41;](../../t-sql/queries/search-condition-transact-sql.md).
154150

155151
CURRENT OF
@@ -164,7 +160,7 @@ DELETE FROM [database_name . [ schema ] . | schema. ] table_name
164160
*cursor_variable_name*
165161
The name of a cursor variable. The cursor variable must reference a cursor that allows updates.
166162

167-
OPTION **(** <query_hint> [ **,**... *n*] **)**
163+
OPTION **(** \<query_hint> [ **,**... *n*] **)**
168164
Keywords that indicate which optimizer hints are used to customize the way the [!INCLUDE[ssDE](../../includes/ssde-md.md)] processes the statement. For more information, see [Query Hints &#40;Transact-SQL&#41;](../../t-sql/queries/hints-transact-sql-query.md).
169165

170166
## Best Practices
@@ -240,11 +236,10 @@ GO
240236
### <a name="LimitRows"></a> Limiting the Rows Deleted
241237
Examples in this section demonstrate how to limit the number of rows that will be deleted.
242238

243-
#### A. Using the WHERE clause to delete a set of rows
239+
#### B. Using the WHERE clause to delete a set of rows
244240
The following example deletes all rows from the `ProductCostHistory` table in the [!INCLUDE[ssSampleDBnormal](../../includes/sssampledbnormal-md.md)] database in which the value in the `StandardCost` column is more than `1000.00`.
245241

246-
```
247-
242+
```
248243
DELETE FROM Production.ProductCostHistory
249244
WHERE StandardCost > 1000.00;
250245
GO
@@ -253,18 +248,16 @@ GO
253248
The following example shows a more complex WHERE clause. The WHERE clause defines two conditions that must be met to determine the rows to delete. The value in the `StandardCost` column must be between `12.00` and `14.00` and the value in the column `SellEndDate` must be null. The example also prints the value from the **@@ROWCOUNT** function to return the number of deleted rows.
254249

255250
```
256-
257251
DELETE Production.ProductCostHistory
258252
WHERE StandardCost BETWEEN 12.00 AND 14.00
259253
AND EndDate IS NULL;
260254
PRINT 'Number of rows deleted is ' + CAST(@@ROWCOUNT as char(3));
261255
```
262256

263-
#### B. Using a cursor to determine the row to delete
257+
#### C. Using a cursor to determine the row to delete
264258
The following example deletes a single row from the `EmployeePayHistory` table in the [!INCLUDE[ssSampleDBnormal](../../includes/sssampledbnormal-md.md)] database using a cursor named `my`_`cursor`. The delete operation affects only the single row currently fetched from the cursor.
265259

266260
```
267-
268261
DECLARE complex_cursor CURSOR FOR
269262
SELECT a.BusinessEntityID
270263
FROM HumanResources.EmployeePayHistory AS a
@@ -281,10 +274,10 @@ DEALLOCATE complex_cursor;
281274
GO
282275
```
283276

284-
#### C. Using joins and subqueries to data in one table to delete rows in another table
277+
#### D. Using joins and subqueries to data in one table to delete rows in another table
285278
The following examples show two ways to delete rows in one table based on data in another table. In both examples, rows from the `SalesPersonQuotaHistory` table in the [!INCLUDE[ssSampleDBnormal](../../includes/sssampledbnormal-md.md)] database are deleted based on the year-to-date sales stored in the `SalesPerson` table. The first `DELETE` statement shows the ISO-compatible subquery solution, and the second `DELETE` statement shows the [!INCLUDE[tsql](../../includes/tsql-md.md)] FROM extension to join the two tables.
286279

287-
```sql
280+
```
288281
-- SQL-2003 Standard subquery
289282
290283
DELETE FROM Sales.SalesPersonQuotaHistory
@@ -295,7 +288,7 @@ WHERE BusinessEntityID IN
295288
GO
296289
```
297290

298-
```sql
291+
```
299292
-- Transact-SQL extension
300293
301294
DELETE FROM Sales.SalesPersonQuotaHistory
@@ -306,22 +299,21 @@ WHERE sp.SalesYTD > 2500000.00;
306299
GO
307300
```
308301

309-
```sql
302+
```
310303
-- No need to mention target table more than once.
311304
312305
DELETE spqh
313306
FROM
314-
Sales.SalesPersonQuotaHistory AS spqh
315-
INNER JOIN Sales.SalesPerson AS sp ON spqh.BusinessEntityID = sp.BusinessEntityID
316-
WHERE
317-
sp.SalesYTD > 2500000.00;
307+
Sales.SalesPersonQuotaHistory AS spqh
308+
INNER JOIN Sales.SalesPerson AS sp
309+
ON spqh.BusinessEntityID = sp.BusinessEntityID
310+
WHERE sp.SalesYTD > 2500000.00;
318311
```
319312

320-
#### A. Using TOP to limit the number of rows deleted
313+
#### E. Using TOP to limit the number of rows deleted
321314
When a TOP (*n*) clause is used with DELETE, the delete operation is performed on a random selection of *n* number of rows. The following example deletes `20` random rows from the `PurchaseOrderDetail` table in the [!INCLUDE[ssSampleDBnormal](../../includes/sssampledbnormal-md.md)] database that have due dates that are earlier than July 1, 2006.
322315

323316
```
324-
325317
DELETE TOP (20)
326318
FROM Purchasing.PurchaseOrderDetail
327319
WHERE DueDate < '20020701';
@@ -342,11 +334,9 @@ GO
342334
### <a name="RemoteTables"></a> Deleting Rows From a Remote Table
343335
Examples in this section demonstrate how to delete rows from a remote table by using a [linked server](../../relational-databases/system-stored-procedures/sp-addlinkedserver-transact-sql.md) or a [rowset function](../../t-sql/functions/rowset-functions-transact-sql.md) to reference the remote table. A remote table exists on a different server or instance of SQL Server.
344336

345-
||
346-
|-|
347-
|**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] through [!INCLUDE[ssCurrent](../../includes/sscurrent-md.md)].|
337+
**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] through [!INCLUDE[ssCurrent](../../includes/sscurrent-md.md)].
348338

349-
#### A. Deleting data from a remote table by using a linked server
339+
#### F. Deleting data from a remote table by using a linked server
350340
The following example deletes rows from a remote table. The example begins by creating a link to the remote data source by using [sp_addlinkedserver](../../relational-databases/system-stored-procedures/sp-addlinkedserver-transact-sql.md). The linked server name, `MyLinkServer`, is then specified as part of the four-part object name in the form *server.catalog.schema.object*.
351341

352342
```
@@ -367,20 +357,22 @@ GO
367357
-- Specify the remote data source using a four-part name
368358
-- in the form linked_server.catalog.schema.object.
369359
370-
DELETE MyLinkServer.AdventureWorks2012.HumanResources.Department WHERE DepartmentID > 16;
360+
DELETE MyLinkServer.AdventureWorks2012.HumanResources.Department
361+
WHERE DepartmentID > 16;
371362
GO
372363
```
373364

374-
#### B. Deleting data from a remote table by using the OPENQUERY function
365+
#### G. Deleting data from a remote table by using the OPENQUERY function
375366
The following example deletes rows from a remote table by specifying the [OPENQUERY](../../t-sql/functions/openquery-transact-sql.md) rowset function. The linked server name created in the previous example is used in this example.
376367

377368
```
378-
DELETE OPENQUERY (MyLinkServer, 'SELECT Name, GroupName FROM AdventureWorks2012.HumanResources.Department
369+
DELETE OPENQUERY (MyLinkServer, 'SELECT Name, GroupName
370+
FROM AdventureWorks2012.HumanResources.Department
379371
WHERE DepartmentID = 18');
380372
GO
381373
```
382374

383-
#### C. Deleting data from a remote table by using the OPENDATASOURCE function
375+
#### H. Deleting data from a remote table by using the OPENDATASOURCE function
384376
The following example deletes rows from a remote table by specifying the [OPENDATASOURCE](../../t-sql/functions/opendatasource-transact-sql.md) rowset function. Specify a valid server name for the data source by using the format *server_name* or *server_name\instance_name*.
385377

386378
```
@@ -392,21 +384,22 @@ WHERE DepartmentID = 17;'
392384

393385
### <a name="CaptureResults"></a> Capturing the results of the DELETE statement
394386

395-
#### A. Using DELETE with the OUTPUT clause
387+
#### I. Using DELETE with the OUTPUT clause
396388
The following example shows how to save the results of a `DELETE` statement to a table variable in the [!INCLUDE[ssSampleDBnormal](../../includes/sssampledbnormal-md.md)] database.
397389

398390
```
399-
400391
DELETE Sales.ShoppingCartItem
401392
OUTPUT DELETED.*
402393
WHERE ShoppingCartID = 20621;
403394
404395
--Verify the rows in the table matching the WHERE clause have been deleted.
405-
SELECT COUNT(*) AS [Rows in Table] FROM Sales.ShoppingCartItem WHERE ShoppingCartID = 20621;
396+
SELECT COUNT(*) AS [Rows in Table]
397+
FROM Sales.ShoppingCartItem
398+
WHERE ShoppingCartID = 20621;
406399
GO
407400
```
408401

409-
#### B. Using OUTPUT with <from_table_name> in a DELETE statement
402+
#### J. Using OUTPUT with <from_table_name> in a DELETE statement
410403
The following example deletes rows in the `ProductProductPhoto` table in the [!INCLUDE[ssSampleDBnormal](../../includes/sssampledbnormal-md.md)] database based on search criteria defined in the `FROM` clause of the `DELETE` statement. The `OUTPUT` clause returns columns from the table being deleted, `DELETED.ProductID`, `DELETED.ProductPhotoID`, and columns from the `Product` table. This is used in the `FROM` clause to specify the rows to delete.
411404

412405
```
@@ -436,22 +429,22 @@ GO
436429

437430
## Examples: [!INCLUDE[ssSDWfull](../../includes/sssdwfull-md.md)] and [!INCLUDE[ssPDW](../../includes/sspdw-md.md)]
438431

439-
### C. Delete all rows from a table
432+
### K. Delete all rows from a table
440433
The following example deletes all rows from the `Table1` table because a WHERE clause is not used to limit the number of rows deleted.
441434

442435
```
443436
DELETE FROM Table1;
444437
```
445438

446-
### D. DELETE a set of rows from a table
439+
### L. DELETE a set of rows from a table
447440
The following example deletes all rows from the `Table1` table that have a value greater than 1000.00 in the `StandardCost` column.
448441

449442
```
450443
DELETE FROM Table1
451444
WHERE StandardCost > 1000.00;
452445
```
453446

454-
### E. Using LABEL with a DELETE statement
447+
### M. Using LABEL with a DELETE statement
455448
The following example uses a label with the DELETE statement.
456449

457450
```
@@ -460,7 +453,7 @@ OPTION ( LABEL = N'label1' );
460453
461454
```
462455

463-
### F. Using a label and a query hint with the DELETE statement
456+
### N. Using a label and a query hint with the DELETE statement
464457
This query shows the basic syntax for using a query join hint with the DELETE statement. For more information on join hints and how to use the OPTION clause, see [OPTION (SQL Server PDW)](http://msdn.microsoft.com/en-us/72bbce98-305b-42fa-a19f-d89620621ecc).
465458

466459
```

0 commit comments

Comments
 (0)