Home » SQL & PL/SQL » SQL & PL/SQL » how to truncate for selected tables (oracle 11g,version:11.1.0,windows XP2)
- how to truncate for selected tables [message #557019] Fri, 08 June 2012 03:58 Go to next message
vickyminugmailcom
Messages: 54
Registered: October 2011
Location: banglore
Member
hi,
could someone help me in this scenario,i have many user tables in my database and i want to truncate
selected tables and how i can overcome this scenario.

i can truncate using truncate table <table_name> but they are 250 tables to truncate,
so,i can't write truncate command for every table.


if i want to truncate first i have to truncate parent table or child table and how i can find parent table and child table
for given tables to truncatein my database


Thanks in advance
VVR
- Re: how to truncate for selected tables [message #557020 is a reply to message #557019] Fri, 08 June 2012 04:21 Go to previous messageGo to next message
Michel Cadot
Messages: 68757
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Quote:
so,i can't write truncate command for every table


Just make a loop on them.

Quote:
if i want to truncate first i have to truncate parent table or child table


Anyway you can't truncate a table which has a forign key pointing to it.
You have to first disable all foreign keys.

Regards
Michel
- icon14.gif  Re: how to truncate for selected tables [message #557023 is a reply to message #557019] Fri, 08 June 2012 05:04 Go to previous messageGo to next message
uni9
Messages: 3
Registered: February 2011
Location: Pune
Junior Member
vickyminugmailcom wrote on Fri, 08 June 2012 17:58
hi,
could someone help me in this scenario,i have many user tables in my database and i want to truncate
selected tables and how i can overcome this scenario.

i can truncate using truncate table <table_name> but they are 250 tables to truncate,
so,i can't write truncate command for every table.


if i want to truncate first i have to truncate parent table or child table and how i can find parent table and child table
for given tables to truncatein my database


Thanks in advance
VVR



This will solve your problem.
{caution: this block will truncate current user tables!!!)

set serveroutput on
begin
FOR REC IN (SELECT table_name FROM user_tables)
LOOP
EXECUTE IMMEDIATE 'Truncate Table ' ||REC.table_name;
DBMS_OUTPUT.PUT_LINE('Table '||REC.table_name||' Truncated!');
END LOOP;
END;
- Re: how to truncate for selected tables [message #557026 is a reply to message #557020] Fri, 08 June 2012 05:16 Go to previous messageGo to next message
vickyminugmailcom
Messages: 54
Registered: October 2011
Location: banglore
Member
how to disable all foreign keys and after truncating how to enable all keys again
- Re: how to truncate for selected tables [message #557031 is a reply to message #557026] Fri, 08 June 2012 05:47 Go to previous messageGo to next message
uni9
Messages: 3
Registered: February 2011
Location: Pune
Junior Member
vickyminugmailcom wrote on Fri, 08 June 2012 19:16
how to disable all foreign keys and after truncating how to enable all keys again

SET serveroutput ON;
/
DECLARE
   /*The name of the schema that should be synchronized.*/
   Schema_Name VARCHAR2(4000) :='type target schema name here';
   /*The operation type:*/
   /*  ON -- enable foreign keys;*/
   /*  OFF -- disable foreign keys.*/
   ON_OFF VARCHAR2(4000) :='ON';
PROCEDURE CONSTRAINTS_ON_OFF
(Target_Schema_Name IN VARCHAR2, Action IN VARCHAR2:='')
IS
   sql_str VARCHAR2(4000);
   FK_name VARCHAR2(4000);
   var_action VARCHAR2(4000);
CURSOR cCur1 IS
   /*Creating the list of foreign keys that should be disabled/enabled,*/
   /*with creating a command at the same time.*/
   SELECT
      'ALTER TABLE '||OWNER||'.'||
      TABLE_NAME||' '||var_action||' CONSTRAINT '||CONSTRAINT_NAME AS sql_string,
      CONSTRAINT_NAME
   FROM
      ALL_CONSTRAINTS
   WHERE
      CONSTRAINT_TYPE='R' AND OWNER=Target_Schema_Name;
BEGIN
   IF upper(Action)='ON' THEN
       var_action :='ENABLE';
   ELSE
       var_action :='DISABLE';
   END IF;
OPEN cCur1;
   LOOP
      FETCH cCur1 INTO SQL_str,fk_name;
      EXIT WHEN cCur1%NOTFOUND;
      /*Disabling/Enabling foreign keys.*/
      EXECUTE IMMEDIATE SQL_str;
      DBMS_Output.PUT_LINE('Foreign key '||FK_name||' is '||var_action||'d');
   END LOOP;
EXCEPTION
WHEN OTHERS THEN
    BEGIN
        DBMS_Output.PUT_LINE(SQLERRM);
    END;
    CLOSE cCur1;
END;
BEGIN
    CONSTRAINTS_ON_OFF(Schema_Name,ON_OFF);
    /*specify additional calls if necessary*/
END;
/
COMMIT;
/
- Re: how to truncate for selected tables [message #557036 is a reply to message #557031] Fri, 08 June 2012 06:17 Go to previous messageGo to next message
cookiemonster
Messages: 13966
Registered: September 2008
Location: Rainy Manchester
Senior Member
That exception handler is stupid and should be removed.
That commit is pointless and should be removed.
You should use a for loop as you did in the previous code.
- Re: how to truncate for selected tables [message #557097 is a reply to message #557036] Fri, 08 June 2012 15:36 Go to previous message
Littlefoot
Messages: 21823
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
@uni9, it is not polite to steal someone else's work. You could have posted a link to the original (dBforge team blog, How To: Disable All Foreign Keys in Oracle Scheme) instead of making people think that you are the author of the code you posted.
Previous Topic: Improving Query performance
Next Topic: how to modify column data
Goto Forum:
  


Current Time: Mon Apr 28 13:41:54 CDT 2025