{"id":195,"date":"2013-08-02T16:39:15","date_gmt":"2013-08-02T21:39:15","guid":{"rendered":"https:\/\/bijoos.com\/oraclenotes\/?p=195"},"modified":"2026-02-07T15:38:09","modified_gmt":"2026-02-07T21:38:09","slug":"a-plsql-program","status":"publish","type":"post","link":"https:\/\/bijoos.com\/oraclenotes\/2013\/195\/","title":{"rendered":"A PL\/SQL Program &#8211; After many many years!"},"content":{"rendered":"<p>Wrote a little pl\/sql program after several years&#8230; glad it worked as expected&#8230; thought of sharing the code, might be useful for someone out there&#8230;<\/p>\n<p>The requirement is to drop tables that are older than 60 days from a schema named CR_BKUP periodically. Did not want to just drop the tables, so taking a backup of the whole schema using data pump api and then dropping the tables. The table names dropped are also written to a log file. The code is created as a procedure, so that I can schedule it using the database scheduler.<\/p>\n<p>If I am violating any coding standards or a better way to accomplish the result, please let me know.<\/p>\n<p>This code is also a quick demonstration of how to use DBMS_DATAPUMP and UTL_FILE programs.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true\" title=\"PL\/SQL Code using DBMS_DATAPUMP and UTL_FILE \">\/* Formatted *\/\nCREATE OR REPLACE PROCEDURE XX_DROP_CR_BKUP_TABLES (\n   threshold_days NUMBER DEFAULT 60)\nIS\n   \/******************************************************************************\n      NAME:       XX_DROP_CR_BKUP_TABLES\n      PURPOSE:    Drop tables under CR_BKUP schema older than 60 days\n\n   ******************************************************************************\/\n\n   CURSOR tablist\n   IS\n      SELECT owner || '.' || object_name table_name\n        FROM dba_objects\n       WHERE     owner = 'CR_BKUP'\n             AND object_type = 'TABLE'\n             AND last_ddl_time &lt; (SYSDATE - threshold_days);\n\n   log_file   UTL_FILE.FILE_TYPE;\nBEGIN\n   -- Export all the tables belonging to CR_BKUP schema to file system\n   -- This dump file will be overwritten next time the job runs\n   -- OS file backup will copy to the backup media\n   DECLARE\n      dp_file   NUMBER;\n      jstatus   VARCHAR2 (200);\n      ltmp      VARCHAR2 (200);\n   BEGIN\n      EXECUTE IMMEDIATE\n         'CREATE OR REPLACE DIRECTORY CR_BKUP_DIR AS ''\/u01\/app\/oracle\/expdp''';\n\n      BEGIN\n         SELECT table_name\n           INTO ltmp\n           FROM user_tables\n          WHERE table_name = 'CR_BKUP_TABLE_EXP';\n\n         EXECUTE IMMEDIATE 'DROP TABLE CR_BKUP_TABLE_EXP';\n      EXCEPTION\n         WHEN NO_DATA_FOUND\n         THEN\n            NULL;\n      END;\n\n      --utl_file.fremove( 'CR_BKUP_DIR', 'cr_bkup_tabs.dmp' );\n      --utl_file.fremove( 'CR_BKUP_DIR', 'cr_bkup_tabs.log' );\n\n      dp_file :=\n         DBMS_DATAPUMP.open (OPERATION   =&gt; 'EXPORT',\n                             JOB_MODE    =&gt; 'SCHEMA',\n                             JOB_NAME    =&gt; 'CR_BKUP_TABLE_EXP');\n\n      DBMS_DATAPUMP.add_file (\n         HANDLE      =&gt; dp_file,\n         FILENAME    =&gt; 'cr_bkup_tabs.dmp',\n         DIRECTORY   =&gt; 'CR_BKUP_DIR',\n         FILETYPE    =&gt; DBMS_DATAPUMP.KU$_FILE_TYPE_DUMP_FILE,\n         REUSEFILE   =&gt; 1);\n      DBMS_DATAPUMP.add_file (\n         HANDLE      =&gt; dp_file,\n         FILENAME    =&gt; 'cr_bkup_tabs.log',\n         DIRECTORY   =&gt; 'CR_BKUP_DIR',\n         FILETYPE    =&gt; DBMS_DATAPUMP.KU$_FILE_TYPE_LOG_FILE);\n      DBMS_DATAPUMP.metadata_filter (HANDLE   =&gt; dp_file,\n                                     NAME     =&gt; 'SCHEMA_LIST',\n                                     VALUE    =&gt; '''CR_BKUP''');\n      DBMS_DATAPUMP.start_job (HANDLE =&gt; dp_file);\n      DBMS_DATAPUMP.wait_for_job (HANDLE =&gt; dp_file, JOB_STATE =&gt; jstatus);\n      DBMS_DATAPUMP.detach (HANDLE =&gt; dp_file);\n   EXCEPTION\n      WHEN OTHERS\n      THEN\n         DBMS_DATAPUMP.detach (HANDLE =&gt; dp_file);\n         RAISE;\n   END;\n\n   -- Export completed, now drop the tables older than the create date threshold\n   --\n   log_file := UTL_FILE.FOPEN ('CR_BKUP_DIR', 'cr_bkup_table_drop.txt', 'W');\n   UTL_FILE.put_line (log_file,\n                      'The following tables dropped on ' || SYSTIMESTAMP);\n\n   FOR rtab IN tablist\n   LOOP\n      EXECUTE IMMEDIATE 'DROP TABLE ' || rtab.table_name;\n\n      UTL_FILE.put_line (\n         log_file,\n         'Dropped at ' || SYSTIMESTAMP || ' : ' || rtab.table_name);\n   END LOOP;\n\n   UTL_FILE.put_line (log_file, 'Completed Drop at ' || SYSTIMESTAMP);\n   UTL_FILE.FCLOSE (log_file);\nEND XX_DROP_CR_BKUP_TABLES;\n\/<\/pre>\n<p>&nbsp;<\/p>\n<p>For this procedure to work, the schema owning this procedure should be granted these privileges explicitly.<\/p>\n<pre class=\"lang:default decode:true \" title=\"Privileges - replace SYSTEM with appropriate schema\">grant select on dba_segments to SYSTEM;\ngrant drop any table to SYSTEM;<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Wrote a little pl\/sql program after several years&#8230; glad it worked as expected&#8230; thought of sharing the code, might be useful for someone out there&#8230; The requirement is to drop tables that are older than 60 days from a schema named CR_BKUP periodically. Did not want to just drop the tables, so taking a backup &#8230; <a title=\"A PL\/SQL Program &#8211; After many many years!\" class=\"read-more\" href=\"https:\/\/bijoos.com\/oraclenotes\/2013\/195\/\" aria-label=\"Read more about A PL\/SQL Program &#8211; After many many years!\">Read more<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[133,22],"tags":[41,71],"class_list":["post-195","post","type-post","status-publish","format-standard","hentry","category-oracledb","category-scripts-tools","tag-dbms_datapump","tag-utl_file"],"acf":[],"jetpack-related-posts":[{"id":2197,"url":"https:\/\/bijoos.com\/oraclenotes\/2018\/2197\/","url_meta":{"origin":195,"position":0},"title":"Oracle Tidbits &#8211; June 2018 #oratidbit","author":"biju.thomas","date":"July 1, 2018","format":false,"excerpt":"\u201cOracle *daily* TidBits\u201d (#oratidbit) published on\u00a0\u00a0Facebook,\u00a0Twitter,\u00a0and\u00a0Google+\u00a0during weekdays\u00a0in June 2018. Hope you find these helpful to learn something new or to remind you of its existence and use. #oratidbit #db18c A shared single client access name (Shared SCAN) enables the sharing of one set of SCAN virtual IPs (VIPs) and Listeners\u2026","rel":"","context":"In &quot;TidBits &amp; Quick Tips&quot;","block_context":{"text":"TidBits &amp; Quick Tips","link":"https:\/\/bijoos.com\/oraclenotes\/category\/tidbits\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/bijoos.com\/oraclenotes\/wp-content\/uploads\/2018\/04\/oratidbit.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1519,"url":"https:\/\/bijoos.com\/oraclenotes\/2015\/1519\/","url_meta":{"origin":195,"position":1},"title":"Oracle Tidbits &#8211; April 2015","author":"Biju","date":"May 1, 2015","format":false,"excerpt":"\u201cOracle *daily* TidBits\u201d (#oratidbit) published at\u00a0https:\/\/www.facebook.com\/oraclenotes\u00a0on weekdays\u00a0in April\u00a02015. You will also see these tidbits, one tidbit at a time, for each page refresh on the right side of this blog as well\u2026 Hope you find these helpful to learn something new or to remind you of its existence and use\u2026\u2026","rel":"","context":"In &quot;TidBits &amp; Quick Tips&quot;","block_context":{"text":"TidBits &amp; Quick Tips","link":"https:\/\/bijoos.com\/oraclenotes\/category\/tidbits\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":352,"url":"https:\/\/bijoos.com\/oraclenotes\/2014\/352\/","url_meta":{"origin":195,"position":2},"title":"Two Cool Utilities in DBMS_UTILITY","author":"Biju","date":"May 18, 2014","format":false,"excerpt":"Oracle Database DBMS_UTILITY package has several useful subprograms. GET_DEPENDENCY shows the objects depended on the object. Here is an example (the procedure uses DBMS_OUTPUT to show the result, hence SET SERVEROUTPUT is required in SQL*Plus): SQL> set serveroutput on SQL> exec dbms_utility.get_dependency('VIEW','HR','EMP_DETAILS_VIEW'); - DEPENDENCIES ON HR.EMP_DETAILS_VIEW ------------------------------------------------------------------ *VIEW HR.EMP_DETAILS_VIEW() PL\/SQL\u2026","rel":"","context":"In &quot;Oracle Database&quot;","block_context":{"text":"Oracle Database","link":"https:\/\/bijoos.com\/oraclenotes\/category\/oracledb\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1516,"url":"https:\/\/bijoos.com\/oraclenotes\/2015\/1516\/","url_meta":{"origin":195,"position":3},"title":"Preventing CREATE (only) for a User","author":"Biju","date":"April 27, 2015","format":false,"excerpt":"The PL\/SQL reference guide has a short section and example for the INSTEAD OF CREATE ON SCHEMA\u00a0trigger.\u00a0Since the description was so little and could not find much detail on the internet, tried some examples as below. This feature is good to prevent any CREATE operation for an user, giving him\/her\u2026","rel":"","context":"In &quot;Oracle Database&quot;","block_context":{"text":"Oracle Database","link":"https:\/\/bijoos.com\/oraclenotes\/category\/oracledb\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1588,"url":"https:\/\/bijoos.com\/oraclenotes\/2015\/1588\/","url_meta":{"origin":195,"position":4},"title":"OCA12c \u2013 1Z0-061 \u2013 Additional Reading 07","author":"Biju","date":"September 5, 2015","format":false,"excerpt":"[Additional] Reading material for 1z0-061 Oracle Database 12c SQL Fundamentals test. Chapter 7: Creating Tables and Constraints\u00a0explains how to create and manage tables and constraints. It also discusses the various data types available in Oracle Database 12c to store data. 10 Introduction to Data Definition Language 10.1 Categorize the main\u2026","rel":"","context":"In &quot;Certification &amp; Learning&quot;","block_context":{"text":"Certification &amp; Learning","link":"https:\/\/bijoos.com\/oraclenotes\/category\/certification-learning\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2210,"url":"https:\/\/bijoos.com\/oraclenotes\/2018\/2210\/","url_meta":{"origin":195,"position":5},"title":"Oracle Tidbits &#8211; July 2018 #oratidbit","author":"biju.thomas","date":"August 1, 2018","format":false,"excerpt":"\u201cOracle *daily* TidBits\u201d (#oratidbit) published on\u00a0\u00a0Facebook,\u00a0Twitter,\u00a0and\u00a0Google+\u00a0during weekdays\u00a0in July 2018. Hope you find these helpful to learn something new or to remind you of its existence and use. #oratidbit #db18c To change a regular user (password authenticated) to schema only account, the user should not have any administrative privileges (SYSDBA, SYSRAC,\u2026","rel":"","context":"In &quot;TidBits &amp; Quick Tips&quot;","block_context":{"text":"TidBits &amp; Quick Tips","link":"https:\/\/bijoos.com\/oraclenotes\/category\/tidbits\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/bijoos.com\/oraclenotes\/wp-content\/uploads\/2018\/04\/oratidbit.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/bijoos.com\/oraclenotes\/wp-json\/wp\/v2\/posts\/195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bijoos.com\/oraclenotes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bijoos.com\/oraclenotes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bijoos.com\/oraclenotes\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bijoos.com\/oraclenotes\/wp-json\/wp\/v2\/comments?post=195"}],"version-history":[{"count":1,"href":"https:\/\/bijoos.com\/oraclenotes\/wp-json\/wp\/v2\/posts\/195\/revisions"}],"predecessor-version":[{"id":3051,"href":"https:\/\/bijoos.com\/oraclenotes\/wp-json\/wp\/v2\/posts\/195\/revisions\/3051"}],"wp:attachment":[{"href":"https:\/\/bijoos.com\/oraclenotes\/wp-json\/wp\/v2\/media?parent=195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bijoos.com\/oraclenotes\/wp-json\/wp\/v2\/categories?post=195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bijoos.com\/oraclenotes\/wp-json\/wp\/v2\/tags?post=195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}