Home » SQL & PL/SQL » SQL & PL/SQL » Want to check if object exist or not while creating procedure(2 Merged) (Oracle 9i, XP)
Want to check if object exist or not while creating procedure(2 Merged) [message #546251] Mon, 05 March 2012 23:50 Go to next message
vinkum
Messages: 19
Registered: October 2011
Location: India
Junior Member

Hi Techies,
I have to create a stored procedure having some 10 cursors and i have to display the data's fetched by select statement in cursors using dbms_output.put_line().

for ex:
-------

CREATE OR REPLACE PROCEDURE PROC AS
CURSOR C1 AS SELECT * FROM EMP;
BEGIN
 FOR R IN C1
 LOOP
     DBMS_OUTPUT.PUT_LINE(R.empno);
     ......
     ......
 END LOOP;
END;


My question is while creating procedure i want to check if the object exist in database or not.
If EXIST
NO PROBLEM MY CODE CREATE THE PROC AND EXECUTED FINE
IF NOT EXIST
I don't want an exception to be thrown stating "OBJECT DOES NOT EXIST" I wanted PL/SQL engine not to execute the particular select statement itself and continue executing other select statements.

The reason why have such kind of wierd requirement is my program displays all the CEMLI objects in any 11.5.10 instance. I don't know whether they have installed discoverer or not. if they installed it no problem else my program have to eliminate.

Please tell me the solution except
EXECUTE IMMEDIATE, REF CURSOR becoz i tried it and works fine.but not able to wrap in WRAPPER UTILITY 8.0 versions.




Re: Want to check if object exist or not while creating procedure [message #546257 is a reply to message #546251] Tue, 06 March 2012 00:26 Go to previous messageGo to next message
xpact83
Messages: 225
Registered: October 2008
Location: philippines
Senior Member
why don't you first check the object name here.

 SELECT * FROM ALL_OBJECTS 

Re: Want to check if object exist or not while creating procedure [message #546258 is a reply to message #546257] Tue, 06 March 2012 00:33 Go to previous messageGo to next message
vinkum
Messages: 19
Registered: October 2011
Location: India
Junior Member

hi xpact,
Thanks for the reply...

see PL/SQL engine doesn't execute any SQL statement while creating the procedure. It just check for syntax errors and object existence.

I am getting you.
select count(*) into lc_table from all_objects
where table_name='EUL5_DOCUMENTS';

if(lc_table>=1)
select statement
end if;

but this won't work buddy. As i said i want to check while creating the procedure object
Re: Want to check if object exist or not while creating procedure [message #546259 is a reply to message #546257] Tue, 06 March 2012 00:39 Go to previous messageGo to next message
rahulvb
Messages: 924
Registered: October 2009
Location: Somewhere Near Equator.
Senior Member
Please check if following snippet can help you...

SQL> CREATE OR REPLACE PROCEDURE SP_RB
  2  AS 
  3  BEGIN
  4  NULL;
  5  END;
  6  /

Procedure created.

Elapsed: 00:00:00.56
SQL> SELECT OBJECT_NAME , OBJECT_TYPE FROM USER_OBJECTS WHERE OBJECT_NAME = 'SP_RB';

OBJECT_NAME                                                        OBJECT_TYPE
-----------------------------------------------------------------  -------------------
SP_RB                                                              PROCEDURE

Elapsed: 00:00:00.78
SQL>   DECLARE 
  2    v_temp     NUMBER := 0 ;
  3    BEGIN
  4    SELECT 1 INTO  v_temp FROM dual where EXISTS 
  5                        ( SELECT  1 FROM  
  6                                 USER_OBJECTS 
  7                          WHERE 
  8                                OBJECT_NAME = UPPER('SP_RB')
  9                            AND OBJECT_TYPE = 'PROCEDURE'
 10                         );
 11                         
 12    IF (v_temp = 1) THEN
 13      BEGIN
 14        DBMS_OUTPUT.PUT_LINE ('PROCEDURE EXISTS') ;
 15      END;
 16    END IF;
 17    EXCEPTION
 18      WHEN NO_DATA_FOUND THEN
 19        v_temp := 0 ;
 20    END ;
 21  /
PROCEDURE EXISTS

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.53




OR Simply Create one and check for the error from your code


SQL> CREATE PROCEDURE SP_RB
  2   AS 
  3   BEGIN
  4   NULL;
  5   END;
  6   /
CREATE PROCEDURE SP_RB
                 *
ERROR at line 1:
ORA-00955: name is already used by an existing object


Elapsed: 00:00:00.75

[Updated on: Tue, 06 March 2012 00:44]

Report message to a moderator

Re: Want to check if object exist or not while creating procedure [message #546260 is a reply to message #546258] Tue, 06 March 2012 00:44 Go to previous messageGo to next message
xpact83
Messages: 225
Registered: October 2008
Location: philippines
Senior Member
well you can put the code in schema level trigger
first, so that you will check it in all_objects if the object exists



BEFORE CREATE
ON SCHEMA
Re: Want to check if object exist or not while creating procedure [message #546263 is a reply to message #546258] Tue, 06 March 2012 00:52 Go to previous messageGo to next message
xpact83
Messages: 225
Registered: October 2008
Location: philippines
Senior Member
vinkum wrote on Tue, 06 March 2012 00:33
hi xpact,
Thanks for the reply...

see PL/SQL engine doesn't execute any SQL statement while creating the procedure. It just check for syntax errors and object existence.

I am getting you.
select count(*) into lc_table from all_objects
where table_name='EUL5_DOCUMENTS';

if(lc_table>=1)
select statement
end if;

but this won't work buddy. As i said i want to check while creating the procedure object


of course i did not say that you will put "select * from all_objects" in your pl sql code I said check first in all_objects to know if the object is already existing then proceed with what you want to do in your procedure.

just like the codes posted by rahulvb


Re: Want to check if object exist or not while creating procedure [message #546266 is a reply to message #546260] Tue, 06 March 2012 01:09 Go to previous messageGo to next message
vinkum
Messages: 19
Registered: October 2011
Location: India
Junior Member

Hi rahulvb,
Thank you. your code executes fine. but i want to create a cursor on the object that we checking. please check the below code

  1  CREATE OR REPLACE PROCEDURE XX_proc_sample AS
  2     v_temp     NUMBER := 0 ;
  3        BEGIN
  4        SELECT 1 INTO  v_temp FROM dual where EXISTS
  5                            ( SELECT  1 FROM
  6                                     ALL_OBJECTS
  7                              WHERE
  8                                    OBJECT_NAME = UPPER('SP_RB')
  9                                AND OBJECT_TYPE = 'TABLE'
 10                            );
 11       IF (v_temp = 1) THEN
 12        DECLARE
 13             CURSOR lcu_c1 IS SELECT * FROM SP_RB;
 14        BEGIN
 15                 FOR R IN lcu_c1
 16     LOOP
 17             DBMS_OUTPUT.PUT_LINE(R.ID);
 18     END LOOP;
 19        END;
 20      END IF;
 21      EXCEPTION
 22        WHEN NO_DATA_FOUND THEN
 23          v_temp := 0 ;
 24*     END ;
SQL> /

Warning: Procedure created with compilation errors.

SQL> show err;
Errors for PROCEDURE XX_PROC_SAMPLE:

LINE/COL ERROR
-------- -----------------------------------------------------------------
13/22    PL/SQL: SQL Statement ignored
13/36    PL/SQL: ORA-00942: table or view does not exist
17/3     PL/SQL: Statement ignored
17/24    PLS-00364: loop index variable 'R' use is invalid

Re: Want to check if object exist or not while creating procedure [message #546268 is a reply to message #546263] Tue, 06 March 2012 01:15 Go to previous messageGo to next message
vinkum
Messages: 19
Registered: October 2011
Location: India
Junior Member

Hi xpact,
ofcourse you are right. The problem is we tech people not going to run this code.This code will be executed by client people.they don't have any knowledge about PL/SQL or SQL. i have to just send a wrapped PL/SQL and SQL.

they will copy two files in C:/
then open SQL Plus
SQL>@C:/CEMLISQL.sql
Program completed successfully.
Please copy the HTML file from C:/ and mail it to us.

inside SQL file i just written
@C:/CEMLIPLSQL.plsql --this code will create the procedure
exec CEMLIPLSQL

I hope u understand my problem.



Re: Want to check if object exist or not while creating procedure [message #546270 is a reply to message #546266] Tue, 06 March 2012 01:23 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
It should be better to tell us what issue are you trying to achieve with this procedure.
In other words, why do you want to create a procedure in the fly without knowing if the target table exists or not?
Which kind of application are trying to develop?

Regards
Michel
Re: Want to check if object exist or not while creating procedure [message #546271 is a reply to message #546266] Tue, 06 March 2012 01:23 Go to previous messageGo to next message
xpact83
Messages: 225
Registered: October 2008
Location: philippines
Senior Member
just change the default value of v_objectname to your table

Note: I only used a table with single column


CREATE OR REPLACE PROCEDURE XX_proc_sample AS
     v_temp     NUMBER := 0 ;
     v_objectname VARCHAR2(20):='TMP_TBL1';
     TYPE CURSORTYPE IS REF CURSOR;
     vCursor          CURSORTYPE;
     COL1  VARCHAR2(100);
  BEGIN
      SELECT 1 INTO  v_temp FROM dual where EXISTS
                           ( SELECT  1 FROM
                                    ALL_OBJECTS
                             WHERE
                                 OBJECT_NAME = v_objectname
                                AND OBJECT_TYPE = 'TABLE'
                           );
     IF (v_temp = 1) THEN
     
           OPEN vCursor FOR 'SELECT * FROM ' || v_objectname ;
           
        BEGIN
             
             LOOP
                
                FETCH vcursor into   COL1  ;
              
                EXIT WHEN vCursor%NOTFOUND;
                
               DBMS_OUTPUT.PUT_LINE(COL1);
               
             END LOOP;
       END;
     END IF;
     
     
      EXCEPTION
       WHEN NO_DATA_FOUND THEN
         v_temp := 0 ;
 END ;

[Updated on: Tue, 06 March 2012 01:29]

Report message to a moderator

Re: Want to check if object exist or not while creating procedure [message #546280 is a reply to message #546271] Tue, 06 March 2012 01:44 Go to previous messageGo to next message
vinkum
Messages: 19
Registered: October 2011
Location: India
Junior Member

hi rahulvb,
Yes Smile that works fine. thanks a lot.
I was using execute immediate,ref cursors and all to achieve this.
ofcourse able to achieve but not able to wrap. yours its perfectly fine. Smile thank you
Re: Want to check if object exist or not while creating procedure [message #546281 is a reply to message #546280] Tue, 06 March 2012 01:49 Go to previous messageGo to next message
vinkum
Messages: 19
Registered: October 2011
Location: India
Junior Member

Hi Michael Cadot,
Quote:
It should be better to tell us what issue are you trying to achieve with this procedure.
In other words, why do you want to create a procedure in the fly without knowing if the target table exists or not?
Which kind of application are trying to develop?


I am writting a PL/SQL code to capture the details of custom objects like tables,PL/SQL,reports,forms,OA pages in any of the oracle apps 11.5.10 instance.so i have written a select statement to find the discoverer details also in my PL/SQL code. as this program can be run in any instance i want to check whether the instance have discoverer related objects.becoz discoverer has to be installed seperately by client.some clients will install it some will not.
This is where i need to check if the object exist or not while creating procedure.
anyway rahulvb given me a solution. I believe it will work. let me check implement it in my code.
Re: Want to check if object exist or not while creating procedure [message #546297 is a reply to message #546271] Tue, 06 March 2012 02:16 Go to previous messageGo to next message
vinkum
Messages: 19
Registered: October 2011
Location: India
Junior Member

Hi XPact83,
Sorry Man Smile
wrongly typed the name. Yes Smile that works fine. thanks a lot.
full credits goes to Xpact83.
Re: Want to check if object exist or not while creating procedure [message #546299 is a reply to message #546281] Tue, 06 March 2012 02:19 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
I understand, you can use something like:

begin
  for rec in (
    select 1 from dual
    where exists (select null from all_objects where <dependent object test>)
  ) loop
    dbms_utility.exec_ddl_statement ('create or replace myproc ...');
  end loop;
end;
/


Regards
Michel
Re: Want to check if object exist or not while creating procedure [message #546315 is a reply to message #546299] Tue, 06 March 2012 02:35 Go to previous message
vinkum
Messages: 19
Registered: October 2011
Location: India
Junior Member

yes michel,
thank you. just aware of this "dbms_utility.exec_ddl_statement"
Previous Topic: Get local time
Next Topic: Timezone
Goto Forum:
  


Current Time: Mon Aug 25 02:50:14 CDT 2025