简体   繁体   中英

PL/SQL Dynamic Table Names

I'm pretty new to Oracle so not entirely sure this is possible, or if perhaps I'm going about it the wrong way but here goes ...

Part of an old feeder script I'm fixing is looping through ~ 20 tables (could change anytime) to populate relevant staging tables. This part is currently very basic:

...

INSERT INTO staging_tbl_1(
     SELECT *
     FROM source_tbl_1    
);

INSERT INTO staging_tbl_2(
         SELECT *
         FROM source_tbl_2    
);

...

Some of the fields in the source database have different constraints etc which means that every now and then it will throw an exception and the feeder will stop. What I'm hoping to do is create a procedure within the existing feeder package to loop through each row in each record before it is inserted and simply wrap it in an exception block. This way it can be logged without causing the feeder to stop.

Essentially I'm chasing something like this:

BEGIN procedure_x(source_record, staging_record)

  -- Perform validation to ensure records exit

  -- Loop through all record rows
  FOR row IN (SELECT * FROM source_record) LOOP

     -- Wrap in exception block

     -- Insert into staging record

     -- Log exception if it occurs
  END LOOP;         
END

I've attempted ref cursors however in order to get them to work I would also need to know the rowtype in advance (from my limited understanding). I've also tried execute immediate however I cannot find a way to loop this in an appropriate way. Are there any other ways to tackle this?

Additional:

I realise that we really should be fixing the source of the problem rather than going about it like this, unfortunately it is far outside my area of influence.

It is possible to do this without making a separate procedure and just wrap all of the table references in a loop, however I'd like to leave this as a last resort.

Oracle has functionality for logging of DML errors. Use it with single SQL statements. Don't go row-by-row and make your processes crawl.

http://docs.oracle.com/cd/B19306_01/server.102/b14231/tables.htm#ADMIN10261

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM