SQL statements use left Outer Join instead of in to delete multiple records

Source: Internet
Author: User

SQL statements use left Outer Join instead of in to delete multiple records

Using the in statement as a condition for deleting multiple records is not only inefficient, but also limited by the maximum number of in sets (1000, an error will be reported if this number is exceeded, if the in statement is used as the condition for deleting multiple records, there is a great risk. To illustrate this problem, define the following two tables for a simple test.

Definition of "faculty table"

Create Table [DBO]. [Dep] (

[Depid] [int] identity (1, 1) not null,

[Name] [char] (20) null,

[Maxmember] [int] Null

) On [primary]

 

"Student table" Definition

Create Table [DBO]. [Student] (

[ID] [int] identity (1, 1) not null,

[Depid] [int] Null,

[Name] [nvarchar] (10) null,

[Age] [int] Null

) On [primary]

 

Now, we need to delete the records of all student tables associated with the maxmember faculty tables greater than 50.

 

Use the in deletion method:

Delete from student where ID

In (select a. ID as ID from student a left Outer Join Dep B on A. depid = B. depid where B. maxmember> 50)

Restrictions: 1. Low Efficiency

2. restricted by the maximum number of in sets being 1000.

 

Solution: Use left Outer Join:

Delete A from student a left Outer Join Dep B on A. depid = B. depid where B. maxmember> 50

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.