You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
91
91
92
92
Common table expressions can also be used with the SELECT, INSERT, UPDATE, and CREATE VIEW statements. For more information, see [WITH common_table_expression (Transact-SQL)](../../t-sql/queries/with-common-table-expression-transact-sql.md).
The alias specified in the FROM *table_source* clause representing the table or view from which the rows are to be deleted.
102
102
103
103
*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)].
107
105
108
106
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.
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 (Transact-SQL)](../../t-sql/statements/create-view-transact-sql.md).
122
120
123
121
*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)].
127
123
128
124
Either the [OPENQUERY](../../t-sql/functions/openquery-transact-sql.md) or [OPENROWSET](../../t-sql/functions/openrowset-transact-sql.md) function, subject to provider capabilities.
129
125
130
-
WITH **(** <table_hint_limited> [... *n*]**)**
126
+
WITH **(**\<table_hint_limited> [... *n*]**)**
131
127
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 (Transact-SQL)](../../t-sql/queries/hints-transact-sql-table.md).
132
128
133
-
<OUTPUT_Clause>
129
+
\<OUTPUT_Clause>
134
130
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 (Transact-SQL)](../../t-sql/queries/output-clause-transact-sql.md).
135
131
136
132
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.
138
134
139
135
This extension, specifying a join, can be used instead of a subquery in the WHERE clause to identify rows to be removed.
- 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.
151
147
152
-
<search_condition>
148
+
\<search_condition>
153
149
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 (Transact-SQL)](../../t-sql/queries/search-condition-transact-sql.md).
The name of a cursor variable. The cursor variable must reference a cursor that allows updates.
166
162
167
-
OPTION **(** <query_hint> [**,**... *n*]**)**
163
+
OPTION **(**\<query_hint> [**,**... *n*]**)**
168
164
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 (Transact-SQL)](../../t-sql/queries/hints-transact-sql-query.md).
169
165
170
166
## Best Practices
@@ -240,11 +236,10 @@ GO
240
236
### <aname="LimitRows"></a> Limiting the Rows Deleted
241
237
Examples in this section demonstrate how to limit the number of rows that will be deleted.
242
238
243
-
#### A. Using the WHERE clause to delete a set of rows
239
+
#### B. Using the WHERE clause to delete a set of rows
244
240
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`.
245
241
246
-
```
247
-
242
+
```
248
243
DELETE FROM Production.ProductCostHistory
249
244
WHERE StandardCost > 1000.00;
250
245
GO
@@ -253,18 +248,16 @@ GO
253
248
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.
254
249
255
250
```
256
-
257
251
DELETE Production.ProductCostHistory
258
252
WHERE StandardCost BETWEEN 12.00 AND 14.00
259
253
AND EndDate IS NULL;
260
254
PRINT 'Number of rows deleted is ' + CAST(@@ROWCOUNT as char(3));
261
255
```
262
256
263
-
#### B. Using a cursor to determine the row to delete
257
+
#### C. Using a cursor to determine the row to delete
264
258
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.
265
259
266
260
```
267
-
268
261
DECLARE complex_cursor CURSOR FOR
269
262
SELECT a.BusinessEntityID
270
263
FROM HumanResources.EmployeePayHistory AS a
@@ -281,10 +274,10 @@ DEALLOCATE complex_cursor;
281
274
GO
282
275
```
283
276
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
285
278
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.
286
279
287
-
```sql
280
+
```
288
281
-- SQL-2003 Standard subquery
289
282
290
283
DELETE FROM Sales.SalesPersonQuotaHistory
@@ -295,7 +288,7 @@ WHERE BusinessEntityID IN
295
288
GO
296
289
```
297
290
298
-
```sql
291
+
```
299
292
-- Transact-SQL extension
300
293
301
294
DELETE FROM Sales.SalesPersonQuotaHistory
@@ -306,22 +299,21 @@ WHERE sp.SalesYTD > 2500000.00;
306
299
GO
307
300
```
308
301
309
-
```sql
302
+
```
310
303
-- No need to mention target table more than once.
#### A. Using TOP to limit the number of rows deleted
313
+
#### E. Using TOP to limit the number of rows deleted
321
314
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.
322
315
323
316
```
324
-
325
317
DELETE TOP (20)
326
318
FROM Purchasing.PurchaseOrderDetail
327
319
WHERE DueDate < '20020701';
@@ -342,11 +334,9 @@ GO
342
334
### <aname="RemoteTables"></a> Deleting Rows From a Remote Table
343
335
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.
344
336
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)].
348
338
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
350
340
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*.
351
341
352
342
```
@@ -367,20 +357,22 @@ GO
367
357
-- Specify the remote data source using a four-part name
368
358
-- in the form linked_server.catalog.schema.object.
369
359
370
-
DELETE MyLinkServer.AdventureWorks2012.HumanResources.Department WHERE DepartmentID > 16;
#### 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
375
366
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.
376
367
377
368
```
378
-
DELETE OPENQUERY (MyLinkServer, 'SELECT Name, GroupName FROM AdventureWorks2012.HumanResources.Department
#### 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
384
376
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*.
385
377
386
378
```
@@ -392,21 +384,22 @@ WHERE DepartmentID = 17;'
392
384
393
385
### <aname="CaptureResults"></a> Capturing the results of the DELETE statement
394
386
395
-
#### A. Using DELETE with the OUTPUT clause
387
+
#### I. Using DELETE with the OUTPUT clause
396
388
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.
397
389
398
390
```
399
-
400
391
DELETE Sales.ShoppingCartItem
401
392
OUTPUT DELETED.*
402
393
WHERE ShoppingCartID = 20621;
403
394
404
395
--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;
406
399
GO
407
400
```
408
401
409
-
#### B. Using OUTPUT with <from_table_name> in a DELETE statement
402
+
#### J. Using OUTPUT with <from_table_name> in a DELETE statement
410
403
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.
411
404
412
405
```
@@ -436,22 +429,22 @@ GO
436
429
437
430
## Examples: [!INCLUDE[ssSDWfull](../../includes/sssdwfull-md.md)] and [!INCLUDE[ssPDW](../../includes/sspdw-md.md)]
438
431
439
-
### C. Delete all rows from a table
432
+
### K. Delete all rows from a table
440
433
The following example deletes all rows from the `Table1` table because a WHERE clause is not used to limit the number of rows deleted.
441
434
442
435
```
443
436
DELETE FROM Table1;
444
437
```
445
438
446
-
### D. DELETE a set of rows from a table
439
+
### L. DELETE a set of rows from a table
447
440
The following example deletes all rows from the `Table1` table that have a value greater than 1000.00 in the `StandardCost` column.
448
441
449
442
```
450
443
DELETE FROM Table1
451
444
WHERE StandardCost > 1000.00;
452
445
```
453
446
454
-
### E. Using LABEL with a DELETE statement
447
+
### M. Using LABEL with a DELETE statement
455
448
The following example uses a label with the DELETE statement.
456
449
457
450
```
@@ -460,7 +453,7 @@ OPTION ( LABEL = N'label1' );
460
453
461
454
```
462
455
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
464
457
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).
0 commit comments