سؤال

I am using SQL Developer Tool to create a procedure that checks if table named TRANSPORT_PRODUCT exists, if it does then truncate it, if it does not then create it.

Scenario 1: when TRANSPORT_PRODUCT does not exists in the schema and i compile the procedure, I get the following error while compiling the procedure

Error(44,2): PL/SQL: SQL Statement ignored
Error(44,14): PL/SQL: ORA-00942: table or view does not exist

Scenario 2: If I create table TRANSPORT_PRODUCT and then create the procedure and run,everything looks fine except that I use the following query inside the procedure to check if the table exists , where variable TABLE_EXISTS in initialised to 0,

select  COUNT(*) INTO   TABLE_EXISTS from    user_tables where   table_name= TABLE_NAME;
DBMS_OUTPUT.PUT_LINE('IF 0 THEN TABLE DOES NOT EXISTS ELSE TABLE EXISTS -'||TO_CHAR(TABLE_EXISTS,99));

A value 48 is stores in TABLE_EXISTS .. that is totally weird to me :(

Scenario 3: If I create TRANSPORT_PRODUCT table and then create the procedure, Drop the TRANSPORT_PRODUCT and run the procedure , I get the following message , I am not sure why is the procedure dependant on the table !

Error report:
ORA-06550: line 1, column 7:
PLS-00905: object XAAL5.EXPORT_PRODUCT is invalid
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

My procedure:

    CREATE OR REPLACE PROCEDURE EXPORT_PRODUCT IS
        TABLE_NAME      VARCHAR2(50) NULL;
        TABLE_EXISTS    INTEGER:=0;
        TRUNC_TABLE     VARCHAR2(50) NULL;
        CREATE_TABLE    VARCHAR2(1000) NULL;
    BEGIN
        DBMS_OUTPUT.PUT_LINE('Procedure Start Time :' || to_char(systimestamp,'DD-MM-YY HH:MM:SS'));

        TABLE_NAME:='TRANSPORT_PRODUCT';

        select  COUNT(*) INTO   TABLE_EXISTS from    user_tables where   table_name= TABLE_NAME;

        DBMS_OUTPUT.PUT_LINE('IF 0 THEN TABLE DOES NOT EXISTS ELSE TABLE EXISTS -'||TO_CHAR(TABLE_EXISTS,99));

        IF (TABLE_EXISTS<>0) THEN
            BEGIN
                DBMS_OUTPUT.PUT_LINE(TABLE_NAME||' TRANSPORT_PRODUCT TABLE EXISTS AND WILL BE TRUNCATED');
                TRUNC_TABLE:='truncate table '|| TABLE_NAME;
                --DBMS_OUTPUT.PUT_LINE(TRUNC_TABLE);
                execute immediate TRUNC_TABLE;
            END;
        ELSE
            BEGIN 
                DBMS_OUTPUT.PUT_LINE(TABLE_NAME ||' DOES NOT EXISTS AND WILL BE CREATED');
                CREATE_TABLE:=
                    'CREATE TABLE TRANSPORT_PRODUCT (
                        ROW_NUMBER      NUMBER,             
                        PRODUCTID       VARCHAR2(20), 
                        PRODUCTNAME     VARCHAR2(100), 
                        OWNER           VARCHAR2(20), 
                        DIVISIONID      VARCHAR2(20), 
                        MERCHLEVEL1     VARCHAR2(20), 
                        MERCHLEVEL2     VARCHAR2(20), 
                        MERCHLEVEL3     VARCHAR2(20), 
                        MERCHLEVEL4     VARCHAR2(20), 
                        STREAMID        VARCHAR2(20), 
                        SPECIFICATION   VARCHAR2(20), 
                        SSS             VARCHAR2(20))';
                execute immediate CREATE_TABLE;
            END;
        END IF;

     -- INSERT PRODUCT INTO TRANSPORT_PRODUCT
        INSERT INTO TRANSPORT_PRODUCT (ROW_NUMBER,PRODUCTID,PRODUCTNAME,OWNER, DIVISIONID,MERCHLEVEL1, MERCHLEVEL2, MERCHLEVEL3,MERCHLEVEL4,STREAMID,SPECIFICATION,SSS) 
        SELECT  ROWNUM,
                PRODUCTID,
                PRODUCTNAME,
                OWNER,
                DIVISIONID,
                MERCHLEVEL1,
                MERCHLEVEL2,
                MERCHLEVEL3,
                MERCHLEVEL4,
                STREAMID,
                SPECIFICATION,
                SSS
        FROM PRODUCT; 

        DBMS_OUTPUT.PUT_LINE('Procedure End Time :' || to_char(systimestamp,'DD-MM-YY HH:MM:SS'));

   exec buildfile('select * from TRANSPORT_PRODUCT where rownum<=500');

      commit;   
    END;
هل كانت مفيدة؟

المحلول

I could see The Scenario 2 problem is that you are using the same procedure parameter name TABLE_NAME than the column name of USER_TABLES. And then the query gives you the total number of tables you have in the schema. TABLE_NAME = TABLE_NAME matchs always.

And I think the same for your procedure. You use the table statically inside the procedure.

Thanks Regards.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top