Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8392427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:31:04+00:00 2026-06-09T19:31:04+00:00

This is going to be a difficult question to get answered which is why

  • 0

This is going to be a difficult question to get answered which is why for 3 days that I have worked on this package (my first package ever) I have been hesitant to ask.

Below is the layout for the spec and body of my package. Before you look at that here is what I am trying to accomplish. I AM CLOSE TO FINISHING so there is no need to fear that this question is not worth your time.

You may see a few of my personal notes to self in the code as well.

My code is incomplete and currently isn’t compiling but before it ceased to compile I can tell you it did not work either. The DROP and CREATE procedures work. NO NEED TO TOUCH THOSE. My main issues are the LOG_PROC, my EXCEPTIONS, my ARCHIVE_ALL_TABLES… as far as I know

Here is what I am trying to do:

Create a package that could be used to ‘archive’ the newly created tables into archive tables in the format “TEST_TABLE_A_13AUG2012”. This package will use a view I created called VW_TEST_TABLES which has this data:

TEST_TABLE_A
TEST_TABLE_B
TEST_TABLE_C
TEST_TABLE_D

This package will need to drop all previously archived tables before it creates new ones. As such, my package will need to have both DROP_ARCHIVE_TABLES and CREATE_ARCHIVE_TABLES procedures within it. In addition to the DROP and CREATE procedures, my package has a main procedure, called ARCHIVE_ALL_TABLES. This is the procedure that would need to be called (for instance by the scheduler) and do the actual archiving. I need to incorporate proper exception handling in these procedures. (e.g. don’t care if the table does not exist when I go to drop it).

Finally, in order to properly track each archival run, I want to build a logging mechanism. To accomplish this, I built a table in my schema called TEST_PACKAGE_LOG_TBL. This table should has the following columns: ARCHIVE_DATE (DATE), TABLE_NAME (VARCHAR2(30)), STATUS_CODE(VARCHAR2(1)), COMMENTS (VARCHAR2(4000)). For each table I archive, I want to log the date, the table name, either ‘S’ for success or ‘E’ for error and, if I encounter an error in the drop or creation of the table, what the SQLERRM was should be displayed.

Finally, my ARCHIVE_ALL_TABLES procedure should check this log table when it is finishing in order to determine if any tables were not archived properly. I created a function ERRORS_FOUND (return boolean) that accepts one IN parameter (today’s date) and checks the log table for errors. If this function returns true, my ARCHIVE_ALL_TABLES procedure should account for this and ‘notify an administrator’ (For now I am leaving this untouched but eventually it will simply account for this with a comment stating that I would notify an admin and place NULL; in the if then end block.)

To summarize, my package structure must contain (at minimum) the following procedures:

ARCHIVE_ALL_TABLES,
DROP_ARCHIVE_TABLE,
CREATE_ARCHIVE_TABLE,
ERRORS_FOUND (function)

--package specification
CREATE OR REPLACE PACKAGE PKG_TEST_TABLES IS

          -- Author  : 
          -- Created : 8/14/2012 8:40:18 AM
          -- Purpose : For storing procedures to drop, create, and archive new tables

          /* Package specification*/
          PROCEDURE ARCHIVE_ALL_TABLES;
          PROCEDURE DROP_ARCHIVE_TABLES; --2nd
          PROCEDURE CREATE_ARCHIVE_TABLES; --1st and call both from archive tables first assuming it works
          PROCEDURE LOG_PROC
          (
                        P_PROCESS_START_TIMESTAMP TIMESTAMP
                       ,P_ARCHIVE_DATE DATE
                       ,P_TABLE_NAME VARCHAR2
                       ,P_STATUS_CODE VARCHAR2
                       ,P_COMMENTS VARCHAR2
          );
          PROCEDURE W(STR VARCHAR2);

          FUNCTION ERRORS_FOUND(P_JOB_RUN_TIMESTAMP TIMESTAMP) RETURN BOOLEAN;

END PKG_TEST_TABLES;


--package body
CREATE OR REPLACE PACKAGE BODY PKG_TEST_TABLES IS
          /* Package body*/

          -------------------------------------------------------------------------------------------------------------------------------------------------------------------
          -------------------------------------------------------------------------------------------------------------------------------------------------------------------

          /* Procedure 'W' is a wrapper for DBMS output. Placed at top of package to make globally available*/
          PROCEDURE W(STR VARCHAR2) IS
                        L_STRING VARCHAR2(4000);
          BEGIN

                        L_STRING := STR;
                        DBMS_OUTPUT.PUT_LINE(STR);
          END;

          -------------------------------------------------------------------------------------------------------------------------------------------------------------------
          -------------------------------------------------------------------------------------------------------------------------------------------------------------------

          PROCEDURE DROP_ARCHIVE_TABLES AS

                        /* Purpose: For dropping previously archived tables so that new ones can be created */

                        L_NO_TABLES_TO_DROP EXCEPTION;
              BEGIN
                        /* Will drop previously archived tables not current ones*/
                            FOR STMT IN (SELECT 'DROP TABLE mySchema.' || TABLE_NAME AS STR
                                     FROM VW_TEST_TABLES
                                     WHERE REGEXP_LIKE(TABLE_NAME, '.+[0...9]'))
                        LOOP
                                      EXECUTE IMMEDIATE STMT.STR; --so that I don't need ';' at the end of each dynamically created SQL

                        END LOOP;

                        W('Done'); --put the W back in here when in package scope

          EXCEPTION
                        WHEN L_NO_TABLES_TO_DROP THEN
                                      NULL;

          END;

          -------------------------------------------------------------------------------------------------------------------------------------------------------------------
          -------------------------------------------------------------------------------------------------------------------------------------------------------------------

          PROCEDURE CREATE_ARCHIVE_TABLES AS
          /* purpose: setting variable to equal the creation of my 4 tables. Recreating the archive tables */

          L_NO_TABLES_TO_CREATE EXCEPTION;
          L_TABLES_NOT_SUCCESSFULLY_CREATED EXCEPTION;

BEGIN

          FOR STMT IN (SELECT 'CREATE TABLE ' || TABLE_NAME || '_' || TO_CHAR(SYSDATE, 'ddMONyyyy') || ' AS SELECT * FROM ' || TABLE_NAME AS STR
                       FROM VW_TEST_TABLES)
          --LOG_PROC( ,TO_CHAR(SYSDATE, 'ddMONyyyy')  , TABLE_NAME  ,'E' ,'TABLE ARCHIVED SUCCESSFULLY')

          LOOP
                        --DBMS_OUTPUT.PUT_LINE(STMT.STR); --want to do a dbms output first before using 'execute immediate'. Hit test, and run it
                        EXECUTE IMMEDIATE STMT.STR; --so that I don't need ';' at the end of each dynamically created SQL

          END LOOP;

--  DBMS_OUTPUT.PUT_LINE('Done'); --put the W back in here when in package scope

EXCEPTION
              WHEN L_NO_TABLES_TO_CREATE THEN
                            NULL; --logging can go here
              --can call logging procedure here for dml don't need execute immediate, just use    insert into
              WHEN L_TABLES_NOT_SUCCESSFULLY_CREATED THEN
                            NULL; --W('ERROR: ' || SQLERRM);
END;

--PROCEDURE IS NOT CREATING TABLES YET

-------------------------------------------------------------------------------------------------    ------------------------------------------------------------------
-------------------------------------------------------------------------------------------------    ------------------------------------------------------------------
PROCEDURE LOG_PROC(P_PROCESS_START_TIMESTAMP TIMESTAMP, P_ARCHIVE_DATE DATE, P_TABLE_NAME     VARCHAR2, P_STATUS_CODE VARCHAR2, P_COMMENTS VARCHAR2) AS

PRAGMA AUTONOMOUS_TRANSACTION;

/* variables */

L_PROCESS_START_TIMESTAMP TIMESTAMP; L_ARCHIVE_DATE DATE; L_TABLE_NAME VARCHAR2(4000);     L_STATUS_CODE VARCHAR2(1); L_COMMENTS VARCHAR2(4000);

BEGIN

L_PROCESS_START_TIMESTAMP := P_PROCESS_START_TIMESTAMP; L_ARCHIVE_DATE := P_ARCHIVE_DATE;     L_TABLE_NAME := P_TABLE_NAME; L_STATUS_CODE := P_STATUS_CODE; L_COMMENTS := P_COMMENTS;

INSERT INTO TEST_PACKAGE_LOG_TBL(PROCESS_START_TIMESTAMP, ARCHIVE_DATE, TABLE_NAME, STATUS_CODE,     COMMENTS) VALUES(L_PROCESS_START_TIMESTAMP, L_ARCHIVE_DATE, L_TABLE_NAME, L_STATUS_CODE, L_COMMENTS);

RETURN;
END;

-------------------------------------------------------------------------------------------------    ------------------------------------------------------------------
-------------------------------------------------------------------------------------------------    ------------------------------------------------------------------

FUNCTION ERRORS_FOUND(P_JOB_RUN_TIMESTAMP TIMESTAMP) RETURN BOOLEAN IS
L_JOB_RUN_TIMESTAMP TIMESTAMP; ERROR_COUNT NUMBER; ERROR_BOOL BOOLEAN;
BEGIN
L_JOB_RUN_TIMESTAMP := P_JOB_RUN_TIMESTAMP;

SELECT COUNT(*) INTO ERROR_COUNT FROM TEST_PACKAGE_LOG_TBL WHERE STATUS_CODE = 'E' AND     PROCESS_START_TIMESTAMP = L_JOB_RUN_TIMESTAMP; IF ERROR_COUNT > 0 THEN ERROR_BOOL := TRUE; ELSE     ERROR_BOOL := FALSE;
END IF;

RETURN ERROR_BOOL;
END;
-------------------------------------------------------------------------------------------------    ------------------------------------------------------------------
-------------------------------------------------------------------------------------------------    ------------------------------------------------------------------

PROCEDURE ARCHIVE_ALL_TABLES AS

/*
                            Original Author: 
                            Created Date: 13-Aug-2012
                            Purpose: To drop all tables before recreating and archiving newly     created tables
                            NOTE: in package - do not use create or replace and 'as' would be     alternative to 'is'
                            */

/*variables*/
L_DROP_ARCHIVE_TABLES VARCHAR2(4000); L_SQL_CREATE_ARCHIVED_TABLES VARCHAR2(4000);     L_PREVENT_SQL_INJECTION
EXCEPTION
;
--L_NOTIFY_ADMINISTRATOR VARCHAR(4000); --TO BE DONE AT A LATER TIME

BEGIN

RETURN;

EXCEPTION

WHEN L_PREVENT_SQL_INJECTION THEN NULL;

WHEN OTHERS THEN W('ERROR: ' || SQLERRM);

END;

-------------------------------------------------------------------------------------------------    ------------------------------------------------------------------
-------------------------------------------------------------------------------------------------    ------------------------------------------------------------------

BEGIN
-- Initialization
/*archive all tables is like my 'driver' that calls drop then create while logging to the table.     Pragma_auto prevents a rollback which would prevent table logging
              FIRST: This package will need to drop all previously archived tables before it   creates new ones. call drop func first*/

/* calling  ARCHIVE_ALL_TABLES */
BEGIN
-- Call the function
NULL;

END;

RETURN;
END PKG_TEST_TABLES;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:31:06+00:00Added an answer on June 9, 2026 at 7:31 pm
    1. Your LOG_PROC is an autonomous transaction, so you need a COMMIT in there.

    2. You define a number of exceptions, but you don’t RAISE them anywhere in your code. For example, I’m guessing you need something like this:

      PROCEDURE CREATE_ARCHIVE_TABLES AS
        L_NO_TABLES_TO_CREATE EXCEPTION;
        l_count number := 0;
      BEGIN
        FOR STMT IN (SELECT ...)
          LOOP
            l_count := l_count + 1;
            EXECUTE IMMEDIATE STMT.STR;
          END LOOP;
        IF l_count = 0 THEN
          RAISE L_NO_TABLES_TO_CREATE;
        END IF;
      EXCEPTION
            WHEN L_NO_TABLES_TO_CREATE THEN
                          NULL; --logging can go here
      END;
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is going to be a stupid question but I have spent an inordinate
This is going to be a vague and obscure question, which is probably due
This is going to be a bit difficult to explain. I'm usually fine with
This is going to sound too silly / too basic - sorry about that,
I know this is going to be something simple that I'm just missing somehow,
This is going to be a pretty long question, so bear with me. I'm
This is a difficult question to articulate, but I'll give it my best: Basically
This might be one of those questions that are difficult to answer, but here
This is abit difficult to word, so I am going to rely mostly on
I have a table (several actually) that I want to get results from with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.