SQL stitching 2 unrelated tables into 2 columns for batch updates to another table

Source: Internet
Author: User
Tags getdate

Update So_master set LOTTERYNO=T2. Lotteryno,updatetime=getdate ()--select SM. Lotteryno,sm. Sysno,t2. Lotterynofrom so_master sminner Join (select Sysno,row_number () over (order by sysno ASC) Rindexfrom So_master where websit Esysno =6 and Soamt >=800 and Lotteryno is null and  OrderDate >= ' 2016/09/22 00:00:00 ' and orderdate< ' 2016/09/ 00:00:00 ' and Status not in ( -1,-2,-3)] T1 on T1. Sysno=sm. Sysnoinner Join (select Lotteryno,row_number () over (order by sysno ASC) Rindexfrom so_master_lottery where  status=0 a nd websitesysno = 6) t2 on T1.rindex=t2.rindexwhere Sm. sysno=1422087 and SM. Lotteryno is null--order by T1. Sysno Asc,t2. Lotteryno ASC

So_master table abbreviation so,so_master_lottery form for short, SML,

1. First query the so table as the update condition, and then use the row_number sort to get the sequence

2. Query the SML table column as the update value, and then use the row_number sort to get the sequence

3. Associate the so table and the SML table with the serial number to obtain the required update conditions and update values, as follows:

Select SM. Lotteryno,sm. Sysno,t2. Lotterynofrom so_master sminner Join (select Sysno,row_number () over (order by sysno ASC) Rindexfrom So_master where websit Esysno =6 and Soamt >=800 and Lotteryno is null and  OrderDate >= ' 2016/09/22 00:00:00 ' and orderdate< ' 2016/09/ 00:00:00 ' and Status not in ( -1,-2,-3)] T1 on T1. Sysno=sm. Sysnoinner Join (select Lotteryno,row_number () over (order by sysno ASC) Rindexfrom so_master_lottery where  status=0 a nd websitesysno = 6) t2 on T1.rindex=t2.rindexwhere Sm. sysno=1422087 and SM. Lotteryno is nullorder by T1. Sysno Asc,t2. Lotteryno ASC

4. Use the SQL execution syntax for bulk update, make an internal connection, update the condition table by updating the primary Table So_master Table Association T1, and then T2 get the updated value, T1 and update the main table So_master through the Foreign Key Association, T2 is T1 according to the T2 Ordinal Association, and then add all the query criteria ,

The main is the column with the updated value Lotteryno

Other SQL Introduction:

The following SQL is updated data according to Row_number, for reference only

Update So_master set Lotteryno =t1. Lotteryno,updatetime=getdate () from So_master sminner join (select Sysno, 168799-row_number () over (order by sysno ASC) Lotteryno from So_master where Websitesysno =6 and Soamt >=800 and lotteryno are null and OrderDate >= ' 2016/09/22 00: 00:00 ' and orderdate< ' 2016/09/24 00:00:00 ' and Status not in ( -1,-2,-3)] T1 on SM. Sysno=t1. Sysnowhere SM. Lotteryno is null

  

SQL stitching 2 unrelated tables into 2 columns for batch updates to another table

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.