use subqueries to restrict what rows in a table will be deleted : In « Query « SQL Server / T-SQL Tutorial

Home
SQL Server / T-SQL Tutorial
1.Query
2.Insert Delete Update
3.Table
4.Table Join
5.Data Types
6.Set Operations
7.Constraints
8.Subquery
9.Aggregate Functions
10.Date Functions
11.Math Functions
12.String Functions
13.Data Convert Functions
14.Analytical Functions
15.Sequence Indentity
16.View
17.Index
18.Cursor
19.Database
20.Transact SQL
21.Procedure Function
22.Trigger
23.Transaction
24.XML
25.System Functions
26.System Settings
27.System Tables Views
28.User Role
29.CLR
SQL Server / T-SQL Tutorial » Query » In 
1.11.6.use subqueries to restrict what rows in a table will be deleted
3>
4CREATE TABLE titles(
5>    title_id       varchar(20),
6>    title          varchar(80)       NOT NULL,
7>    type           char(12)          NOT NULL,
8>    pub_id         char(4)               NULL,
9>    price          money                 NULL,
10>    advance        money                 NULL,
11>    royalty        int                   NULL,
12>    ytd_sales      int                   NULL,
13>    notes          varchar(200)          NULL,
14>    pubdate        datetime          NOT NULL
15)
16> GO
1>
2CREATE TABLE sales(
3>    stor_id        char(4)           NOT NULL,
4>    ord_num        varchar(20)       NOT NULL,
5>    ord_date       datetime          NOT NULL,
6>    qty            smallint          NOT NULL,
7>    payterms       varchar(12)       NOT NULL,
8>    title_id       varchar(80)
9)
10> GO
1insert sales values('1''QA7442.3', '09/13/94', 75'ON Billing','1')
2insert sales values('2''D4482',    '09/14/94', 10'Ne60',    '1')
3insert sales values('3''N914008',  '09/14/94', 20'Ne30',    '2')
4insert sales values('4''N914014',  '09/14/94', 25'Ne30',    '3')
5insert sales values('5''423LL922', '09/14/94', 15'ON Billing','3')
6insert sales values('6''423LL930', '09/14/94', 10'ON Billing','2')
7> GO

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)
1>
2>      DELETE    sales
3>      WHERE     title_id IN
4>                (SELECT    title_id from titles
5>                WHERE      pub_id = '1389')
6>
7>
8> GO

(rows affected)
1>
2> drop table sales;
3>
4> drop table titles;
5> GO
1.11.In
1.11.1.The syntax of the WHERE clause with an IN phrase
1.11.2.An IN phrase with a list of numeric literals
1.11.3.An IN phrase preceded by NOT
1.11.4.An IN phrase with a subquery
1.11.5.In and subquery
1.11.6.use subqueries to restrict what rows in a table will be deleted
1.11.7.An IN operator is equivalent to a series of conditions, connected with one or more OR operators.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.