MSSQL SQL Efficient Correlated subquery Update batch Update

Source: Internet
Author: User
Tags mssql

/* Update--1 using an update with the associated subquery . Create a test table

Create TABLE Table1 ( a varchar (ten), B varchar (ten), C varchar (ten), CONSTRAINT [pk_ Table1] PRIMARY KEY CLUSTERED ( a ASC ) ) on [PRIMARY]

 create TABLE table2     (     a varchar (ten),      C varchar (ten),      CONSTRAINT [pk_table2] PRIMARY KEY Clustered     (     a ASC < Span class= "Apple-converted-space" >   )     ) on [ Primary]     go     

< Span class= "Apple-converted-space" >< Span class= "Apple-converted-space" >< Span class= "Apple-converted-space" >--2. Creating test Data     

< Span class= "Apple-converted-space" >< Span class= "Apple-converted-space" >< Span class= "Apple-converted-space" >insert into Table1 values (' Zhao ', ' ASDs ', null)     

< Span class= "Apple-converted-space" >< Span class= "Apple-converted-space" >< Span class= "Apple-converted-space" > Insert into Table1 values (' money ', ' ASDs ', ' + ')     

< Span class= "Apple-converted-space" >< Span class= "Apple-converted-space" >< Span class= "Apple-converted-space" >< Span class= "Apple-converted-space" >insert into Table1 values (' Sun ', ' ASDs ', ' ')      

Insert into Table1 values (' Lee ', ' ASDs ', null)

Insert into Table2 values (' Zhao ', ' all ')

Insert into Table2 values (' money ', ' + ')

Insert into Table2 values (' sun ', ' + ')

Insert into Table2 values (' Li ', ' up ')

GO SELECT * from Table1

--3. Updating with update mode

Update Table1 Set c = (select C from Table2 where a = table1.a) where c is null GO

--4. Displaying the updated results

SELECT * FROM Table1

GO

--5. Deleting a test table

Drop TABLE Table1

Drop TABLE Table2

MSSQL SQL Efficient Correlated subquery Update batch Update

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.