UPSERT into table with dynamic table name

前端 未结 3 733
北荒
北荒 2020-12-11 14:27

Any better method to UPSERT into a table, provided :

  • Data upsert at ~1 row/second
  • Table Name is DYNAMIC, generated using ObjectID parameter passed to
3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-11 14:55

    You should consider writing this to use Static SQL instead of passing the table name at Run Time. Is there a valid reason why you don't know what table you would be merging into till run-time?

    As for debugging the issue...

    How is the function FGETTABLENAME defined in your code? This is what I came up with which mimics that scenario. I would suggest Using %type (instead of RAW for Number Types) declarations and removing the Double Quotes from the procedure Names.

        create or replace function FGETTABLENAME(
            POBJECTID in user_objects.object_id%type
        ) return user_objects.object_name%type
        as
          v_object_name user_objects.object_name%type;
        begin
          select object_name
            into v_object_name
            from all_objects
            where object_id = pobjectid;
           return v_object_name;
        end;
        /
    
    SQL> select object_id, object_name from user_objects;
    
     OBJECT_ID OBJECT_NAME
    ---------- --------------------------------------------
         52641 TFIVE
         52644 SPINSERTDATA
         52643 PROCEDURE
         52645 FGETTABLENAME
         52554 GET_SAL_EMP
         52559 T1
    
    SQL> select FGETTABLENAME(52641) from dual;
    
    FGETTABLENAME(52641)
    --------------------------------------------
    TFIVE
    

    You can add DBMS_OUTPUT.PUT_LINE statements to your code after

    vTableName := FGETTABLENAME(POBJECTID => pObjectID); 
    
    and 
    
    vQueryUpdate := 'UPDATE '      || vTableName || ' SET "VALUE" = :1';
          vQueryInsert := 'INSERT INTO ' || vTableName || ' ("DTTIME", "VALUE") VALUES (:1, :2)';
    

    or Trace your code to see the actual SQL statements being fired to your Database.

提交回复
热议问题