简体   繁体   中英

create table name with other table column and sequence number oracle

Creating a clear log procedure, which deletes and insert into a backup table everyday, what I need to do is after the backup table reaches a certain number of rows, I want my job(procedure) to create a new table with the same fields.

So I have a table config with the tables I need to backup identified , and I have a column named pre_bck which has the name I want my back up table to have.

So I want to try to make a procedure that will create a table from that 'column'+the sequence number

create table 'column_name'+sequence_id as select * from xyz where 1=0;

so for example if table name was abc and sequece id was 3, table name would be abc3

I dont know if I was clear, as to what I pretend to implement, any help would be appreciated.

I second Alex and Boneist's opinion that this is probably not a good approach.

However, to answer your question:

The operator to glue together various bits of a string is || , not + as in other languages: column_name || sequence_id column_name || sequence_id .

To execute SQL from within a procedure, you can use EXECUTE IMMEDIATE 'CREATE ...';

To select from a sequence, you need to create it with CREATE SEQUENCE my_sequence; . To get a value: my_sequence.nextval .

The easiest way to loop over all rows in your config table is a FOR IN ... LOOP :

CREATE OR REPLACE PROCEDURE my_procedure IS
  stmt VARCHAR2(32000);
BEGIN
  FOR r IN (SELECT * FROM my_config_table ORDER BY xxx) LOOP
    stmt := 'CREATE TABLE ' || pre_bck || my_sequence.nextval || 
            ' AS SELECT * FROM xyz WHERE 1=0';
    EXECUTE IMMEDIATE stmt;
  END LOOP;
END my_procedure;
/

I'm not clear what the xyz part means, but you can surely take it from here...

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