esProc Performs Dynamic Cross-database MERGE Operation

Uncategorized 889 0

The MERGE statement provided by databases like MSSQL and ORACLE is very convenient for updating tables. But it is not as convenient as it is expected to be when the source table and target table exist in different databases. In this case esProc is able to rise to the occasion and assists the operation.

source and target are parameters representing two tables of the same structure but of different data in two databases. The source table will be used to update the target table based on their primary keys. For example, both Table 1 and Table 2 (as shown below) have a primary key consisting of column A and column B:

esProc_sql_dynamic_merge_1

After Table 1 is updated by Table 2, it will be as follows:

esProc_sql_dynamic_merge_2

esProc code:

A B
1 =myDB2.query(“select COLUMN_NAME from INFORMATION_SCHEMA.KEY_COLUMN_USAGE k where k.TABLE_NAME='”+source+”‘”)
2 =pks=A1.(COLUMN_NAME).string@d()
3 =myDB2.cursor(“select * from “+source+” order by “+pks)
4 =myDB1.cursor(“select * from “+target+” order by “+pks)
5 =join@x1(A3,${pks};A4,${pks})
6 for A5,1000 =A6.select( !_2).(_1)
7 =myDB1.update@i(B6,${target};${pks})
8 =A6.select( _2 && !(_1.array() == _2.array())).(_1)
9 =myDB1.update@u(B8,${target};${pks})

A1,A2:Get the source table’s primary key from the system tables and store it in variable pks; the result is “A,B”. Databases vary in how to get the primary key. Here MSSQL will be used as an example. myDB2/myDB1 represents the database where source/target resides.

A3,A4:Retrieve data from source and target as cursors; sort data according to the merging field (the primary key) for the subsequent MERGE operation.

A5:Perform a left-join with target and source. @x represents cursor-handling and @1 represents the left-join. The macro ${columns} is used to convert a string to an expression.

A6:Fetch data from A5’s cursor by loop, 1,000 rows each time. A6 is used in the loop body B6-B9 to reference the loop variable. Below is the structure of the operation performed in A6:

esProc_sql_dynamic_merge_3

B6,B7:Select rows need to be inserted and modify the target. @i option means performing only the INSERT, without scanning the whole table.

B8,B9:Select rows for updating the table and modify the target. @u means performing only the UPDATE. array function gets a list of the field names.

When the loop is over, target (Table 1) has been modified as follows:

esProc_sql_dynamic_merge_4

The above approach also applies to databases that don’t support the MERGE statement, like MySQL.

FAVOR (0)
Leave a Reply
Cancel
Icon

Hi,You need to fill in the Username and Email!

  • Username (*)
  • Email (*)
  • Website