shlogg · Early preview
Judy @esproc_spl

How To Insert Discontinuous Records Into OLDTABLE Based On RANK

Inserting records from NEWTABLE into OLDTABLE based on RANK values, ensuring continuity and updating ranks accordingly. Solution involves using esProc script p1.dfx to perform SQL queries and insertions.

Problem description & analysis
There is a OLDTABLE in a database. Its RANK column contains continuous numbers beginning from 1, as shown below:
There is also a NEWTABLE in the database. Its RANK table contains discontinuous numbers that included in OLDTABLE’s RANK, as shown below:

We are trying to insert records of NEWTABLE into OLDTABLE according to the orders of RANK in the two tables and the record after the newly-inserted record has the rank RANK+1, as shown below:

Solution
Write the following script p1.dfx in esProc:

Explanation:
A1   Connect to the database named demo.
A2  Perform SQL...