I have 3 database located in different schema, lets call it
source_schema.target_tabletarget_A.target_tabletarget_B.target_table
and wanted to move data from table source_schema to target_A, target_B.
Data in source_schema is judged by source_schema.target_table.target_column using a stored function that return the schema target (A or B).
I tried copying only the id and target_col_val into a new table, and run loop through it, and delete the top rows of the new table as the process ended, but I'm worried about performance, so I gave up on this solution since it require lots of SELECT and DELETE.
Right now this is what I have:
DECLARE target_schema VARCHAR(100);
DECLARE target_col_val VARCHAR(100);
DECLARE target_column VARCHAR(100);
DECLARE tempid VARCHAR(100);
DECLARE cur CURSOR FOR SELECT Id, target_column FROM A.target_table INTO tempid, target_col_val;
OPEN cur;
curloop: LOOP
DO
-- THIS PART OF THE CURSOR LOOP LOGIC
-- FECTH(?)
SET target_schema = get_target_schema(target_table, target_column, target_col_val)
-- UPSERT
INSERT INTO target_schema.target_table (id, name, etc)
SELECT id, name, etc FROM A
WHERE id = tempid;
END LOOP;
After a day of thinking, I reached to this answer and it works like a charm. Hopefully this can help anyone with same problem