Truncate Table in a Procedure

Posted on 2005-05-02
Last Modified: 2008-01-09
hi all,
How do I run a truncate command in a Procedure. the user running the procedure will own the table I will be truncating.
Question by:rxs0005
    LVL 76

    Expert Comment

    by:slightwv (䄆 Netminder)
    Not sure of the exact requirements but truncating a table in a procedure may not be the best approach.  For example, you will need to guarantee that  the procedure is only executed one at a time.  Again, not knowing the requirements, have you looked into global temporary tables?

    All that said (untested but should work):

    execute immediate 'truncate table table_name';
    LVL 5

    Expert Comment


    for all ddl commends to execute in procedures u have to use execute immediate with them

    called dynamic sqls

    LVL 4

    Expert Comment

    The main advise is to carefully revise the requirements about this truncate statement.
    The only option to use in a procedure is the dynamic execute immediate already suggested.
    However, if you have foreign keys to this table, you might face the following error:

    ORA-02266 unique/primary keys in table referenced by enabled foreign keys
    Cause: An attempt was made to drop or truncate a table with unique or
    primary keys referenced by foreign keys enabled in another table.
    Action: Before dropping or truncating the table, disable the foreign key
    constraints in other tables. You can see what constraints are referencing a table
    by issuing the following command:
    select constraint_name, table_name, status
    from user_constraints
    where r_constraint_name in (
    select constraint_name from user_constraints where table_name
    Pay attention it does not metter if there are actually childs or not, it is enough to have a enabled foreign key and your statement will fail.
    LVL 4

    Accepted Solution

    Using Execute Immediate you can run ddl commands inside a pl/sql procedure....

    eg.. execute immediate 'truncate table table_name';

    Or you can do this in other way using oracle supplied dbms_sql package.......

    create or replace procedure procName
    crsor integer;
    rval  integer;
     crsor := dbms_sql.open_cursor;
     dbms_sql.parse(crsor, 'truncate table tblname REUSE STORAGE ,dbms_sql.v7);
     rval := dbms_sql.execute(crsor);


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    PRTG Network Monitor: Intuitive Network Monitoring

    Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

    Suggested Solutions

    Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
    This post first appeared at Oracleinaction  ( Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
    This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
    This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now