Most of the code comes from this answer and its basic algorithm is already described in this OTN forum post.
CREATE OR REPLACE PROCEDURE sp_truncate AS BEGIN -- Disable all constraints FOR c IN (SELECT c.owner, c.table_name, c.constraint_name FROM user_constraints c, user_tables t WHERE c.table_name = t.table_name AND c.status = 'ENABLED' ORDER BY c.constraint_type DESC) LOOP DBMS_UTILITY.EXEC_DDL_STATEMENT('ALTER TABLE ' || c.owner || '.' || c.table_name || ' disable constraint ' || c.constraint_name); DBMS_OUTPUT.PUT_LINE('Disabled constraints for table ' || c.table_name); END LOOP; -- Truncate data in all tables FOR i IN (SELECT table_name FROM user_tables) LOOP EXECUTE IMMEDIATE 'TRUNCATE TABLE ' || i.table_name; DBMS_OUTPUT.PUT_LINE('Truncated table ' || i.table_name); END LOOP; -- Enable all constraints FOR c IN (SELECT c.owner, c.table_name, c.constraint_name FROM user_constraints c, user_tables t WHERE c.table_name = t.table_name AND c.status = 'DISABLED' ORDER BY c.constraint_type) LOOP DBMS_UTILITY.EXEC_DDL_STATEMENT('ALTER TABLE ' || c.owner || '.' || c.table_name || ' enable constraint ' || c.constraint_name); DBMS_OUTPUT.PUT_LINE('Enabled constraints for table ' || c.table_name); END LOOP; COMMIT; END sp_truncate; /
In case you want to be able to issue a rollback then you should change the TRUNCATE TABLE (a DDL statement) to a DELETE FROM (a DML statement) so that Oracle can recover the data from its redo logs.
Executing this stored procedure is as simple as:
exec sp_truncate;
Awesome just what I have been looking for. :-)
ReplyDeleteNice one...
ReplyDeleteIt's been nearly 2 years since I wrote this and I'm glad it helped you!
DeleteThank You. It saved me so much time.
ReplyDeleteI hit constraint dependencies trying to use the code above. SO has the answer: http://stackoverflow.com/a/5075815/304282
ReplyDeleteHowever that is quite slow and produces many duplicates, so I created a procedure to generate an ordered table of constraints + I simply loop over that table to enable/disable constraints.
Thanks man, exactly what I have looked for.
ReplyDeleteOracle...each release brings new "surprises"...
oracle course in chennai
Nice article I was really impressed by seeing this blog, it was very interesting and it is very useful for me.
ReplyDeleteWeb Designing Training in Chennai
Web Designing Course in Chennai
Web Designing Training in Bangalore
Web Designing Course in Bangalore
Web Designing Training in Hyderabad
Web Designing Course in Hyderabad
Web Designing Training in Coimbatore
Web Designing Training
Web Designing Online Training
mmorpg
ReplyDeleteİNSTAGRAM TAKİPÇİ SATİN AL
TİKTOK JETON HİLESİ
tiktok jeton hilesi
antalya saç ekimi
referans kimliği nedir
instagram takipçi satın al
metin2 pvp serverlar
instagram takipçi satın al
Smm panel
ReplyDeleteSMM PANEL
is ilanlari blog
İnstagram takipçi satın al
hirdavatciburada.com
beyazesyateknikservisi.com.tr
servis
Jeton hile indir
özel ambulans
ReplyDeleteuc satın al
lisans satın al
nft nasıl alınır
minecraft premium
en son çıkan perde modelleri
en son çıkan perde modelleri
yurtdışı kargo
Good text Write good content success. Thank you
ReplyDeletekralbet
slot siteleri
mobil ödeme bahis
betpark
poker siteleri
tipobet
kibris bahis siteleri
bonus veren siteler
bilecik
ReplyDeletebüyükçekmece
demre
düzce
esenler
Q1Q
yurtdışı kargo
ReplyDeleteresimli magnet
instagram takipçi satın al
yurtdışı kargo
sms onay
dijital kartvizit
dijital kartvizit
https://nobetci-eczane.org/
Z2G
resimli magnet
ReplyDeleteresimli magnet
çerkezköy çatı ustası
silivri çatı ustası
dijital kartvizit
1KPB0X
bayburt
ReplyDeletebeykoz
beyoğlu
bilecik
bingöl
6U7PR
gebze
ReplyDeletebolu
bağcılar
başakşehir
kartal
41L
mecidiyeköy
ReplyDeletemaltepe
beşiktaş
alsancak
adana
H83
adana
ReplyDeleteadıyaman
afyon
aksaray
amasya
antalya
antep
XCU635
web postegro
ReplyDeletetakipcimx 1000
techy hit tools
postegro
takipçi satın al
WM0JEW