Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Basic anonymous block which drops and creates a table

FountainHeadJul 22 2013 — edited Jul 22 2013

Version: 11.2.0.3

I am fairly new to PL/SQL.

We have a table named CHK_CNFG_DTL.

I want to create a backup table for CHK_CNFG_DTL which will be named like CHK_CNFG_DTL_BKP_<timestamp> eg: CHK_CNFG_DTL_BKP_JULY_22_2013

Creation of this backup table has to be automated so, I want to create an anonymous block which will first drop the existing backup table and then create a new backup table from the original table.

The below code works fine. But the very first time when you run it , the loop won't iterate because there is no such table named CHK_CNFG_DTL_BKP%.

declare

v_stmt varchar2(1000);

v_date date;

begin

  for rec in

  (select * from user_tables where table_name like 'CHK_CNFG_DTL_BKP%' )

    loop

        begin

            execute immediate 'alter session set nls_date_format=''DD_MON_YYYY''';

            v_stmt := 'drop table '||rec.table_name|| ' purge';

            dbms_output.put_line(v_stmt);   ----- Drops Old backup table

            execute immediate v_stmt;

            select sysdate into v_date from dual;

            v_stmt := 'create table CHK_CNFG_DTL_BKP_'||to_date(v_date)||' as select * from CHK_CNFG_DTL';

            dbms_output.put_line('Creating Bkp table CHK_CNFG_DTL_BKP_'|| to_date(v_date) );

            dbms_output.put_line(v_stmt);

            execute immediate v_stmt;  --- Creates new Backup table

        exception

        when others

        then

        dbms_output.PUT_LINE (rec.table_name||'-'||sqlerrm);

        end;

    end loop;

end;

/

PL/SQL procedure successfully completed.

-- Backup table not created.

SQL> select table_name from user_Tables where table_name like 'CHK_CNFG_DTL%';

TABLE_NAME

------------------------------

CHK_CNFG_DTL

Of course, this can fixed by creating a table like bleow before executing the anonymous block

SQL> create table CHK_CNFG_DTL_BKP_JULY_22_2013 (x varchar2(37));

Table created.

and now the block will succesfully run like

24  end;

25  /

drop table CHK_CNFG_DTL_BKP_JULY_22_2013 purge

Creating Bkp table CHK_CNFG_DTL_BKP_22_JUL_2013

create table CHK_CNFG_DTL_BKP_22_JUL_2013 as select * from CHK_CNFG_DTL

PL/SQL procedure successfully completed.

But this is going to production . We can't a table like CHK_CNFG_DTL_BKP_JULY_22_2013 without a proper business reason.

How can I modify the above code so that if even if there is no such table like 'CHK_CNFG_DTL_BKP%' , it will proceed to create the backup table?

This post has been answered by Pacmann on Jul 22 2013
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 19 2013
Added on Jul 22 2013
5 comments
2,609 views