Advertisement
Guest User

Untitled

a guest
Jun 4th, 2017
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PL/SQL 2.59 KB | None | 0 0
  1. DECLARE
  2.  
  3.   /*
  4.    * Instanciate the variables to run the PL/SQL script
  5.    */
  6.   my_table VARCHAR2(30) := 'abaco';                     -- Table name
  7.   my_date DATE := SYSDATE - 10;                         -- Date
  8.   -- Boolean variable used to store the value checking the table's existence
  9.   table_exists INTEGER;                                
  10.  
  11.   /*
  12.    * Procedure to remove old records from a main table and store them in a
  13.    * backup table
  14.    */
  15.   PROCEDURE backup_records_before (table_name VARCHAR2, backup_date DATE) IS
  16.  
  17.     /*
  18.      * Instanciate the procedure internal variables
  19.      */
  20.     my_query VARCHAR2(300);        -- Dynamic SQL query
  21.     table_name_backup VARCHAR(34); -- Name of the backup table
  22.     number_condition NUMBER := 5;  -- Backup condition value
  23.  
  24.   BEGIN
  25.  
  26.     -- Print out the passed arguments
  27.     DBMS_OUTPUT.PUT_LINE('The table name is:');
  28.     DBMS_OUTPUT.PUT_LINE(table_name);
  29.     DBMS_OUTPUT.PUT_LINE('The backup date is:');
  30.     DBMS_OUTPUT.PUT_LINE(backup_date);
  31.    
  32.     -- Concat the table name with '_bck' to get the new backup name
  33.     table_name_backup := table_name || '_bck';
  34.    
  35.     -- Print out the backup table name
  36.     DBMS_OUTPUT.PUT_LINE('The backup table name is:');
  37.     DBMS_OUTPUT.PUT_LINE(table_name_backup);
  38.    
  39.     -- Create the query to create the backup table and execute it
  40.     my_query := 'CREATE TABLE ' || table_name_backup || ' AS (SELECT * FROM ' || table_name || ' WHERE table_date < ' || backup_date || ')';
  41.     EXECUTE IMMEDIATE my_query;
  42.    
  43.     -- Print out
  44.     DBMS_OUTPUT.PUT_LINE('Backup table created!');
  45.  
  46.   END backup_records_before;
  47.  
  48. BEGIN
  49.  
  50.   -- Check the existence of the main table and store the result in table_exists
  51.   SELECT COUNT(*) INTO table_exists FROM USER_TABLES WHERE TABLE_NAME = my_table;
  52.  
  53.   -- Print out
  54.   DBMS_OUTPUT.PUT_LINE('The main table exists:');
  55.  
  56.   -- If the main table is not existing
  57.   IF table_exists = 0 THEN
  58.  
  59.     -- Print out
  60.     DBMS_OUTPUT.PUT_LINE('NO');
  61.  
  62.     -- Create the table
  63.     EXECUTE IMMEDIATE 'CREATE TABLE ' || my_table || '(table_id NUMBER PRIMARY KEY, table_date DATE)';
  64.    
  65.     -- Print out
  66.     DBMS_OUTPUT.PUT_LINE('Main table created!');
  67.    
  68.     -- Fill the table with numbers and dates
  69.     FOR n IN 1 .. 20
  70.     LOOP
  71.       EXECUTE IMMEDIATE 'INSERT INTO ' || my_table || ' (table_id, table_date) VALUES (' || n ||', SYSDATE - '|| n || ')';
  72.     END LOOP;
  73.    
  74.   END IF;
  75.  
  76.   -- Print out
  77.   DBMS_OUTPUT.PUT_LINE('YES');
  78.  
  79.   -- Remove the old records from the created table
  80.   backup_records_before (my_table, my_date);
  81.  
  82. END;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement