13

I have searched the net and I've found a post that uses the following snippet to check if a stored procedure exists:

select * 
  from USER_SOURCE 
 where type='PROCEDURE' 
   and name='my_stored_procedure.' 

Is there any other way to check if a procedure exists?

Edited to add:

Before posting SQL Server ways, please I'm looking for ORACLE ways.

Paulo Santos
  • 11,285
  • 4
  • 39
  • 65

7 Answers7

41

Alternatives:

USER_PROCEDURES:

SELECT *
  FROM USER_PROCEDURES
 WHERE object_name = 'MY_STORED_PROCEDURE'

USER_OBJECTS:

SELECT *
  FROM USER_OBJECTS
 WHERE object_type = 'PROCEDURE'
   AND object_name = 'MY_STORED_PROCEDURE'
Chandu
  • 81,493
  • 19
  • 133
  • 134
  • 4
    +1.. the other views to remember: `ALL_OBJECTS`, `DBA_OBJECTS` (if you have that priv). Same for `_PROCEDURES` too. – Guru Apr 19 '11 at 19:50
  • 2
    If you have an invalid procedure (due to compiling errors), the invalid procedure is listet in `USER_OBJECTS` and `ALL_OBJECTS` but not listed in `USER_PROCEDURES` and `ALL_PROCEDURES`. – Peter A Apr 24 '18 at 09:36
  • 1
    what if stored procedure is inside package? – FrenkyB Aug 10 '20 at 06:29
  • this only shows YOUR procs/objects, not all –  Nov 25 '21 at 16:52
7

Something that worked for me!

SELECT text
FROM all_source
WHERE name = 'MY_SP_NAME'
ORDER BY line;

Alternatively you can try calling SP like this:

CALL MY_SP_NAME();

You might end up error like this, but that confirms you have SP defined there:

OCI Statement Execution failure.ORA-06553: PLS-306: wrong number or types of arguments in call to 'MY_SP_NAME'
Waqar Alamgir
  • 9,828
  • 4
  • 30
  • 36
3

The only way to see if a procedure exists in the database is though querying DBA_OBJECTS. The disadvantage here is that only a dba has access to this view. Second best is using all_objects. ALL_OBJECTS shows you the objects for which you have somehow a privilege. USER_OBJECTS only shows you your own objects.

2

Execute the query below in SQL*PLUS, ODBC Test,...

SELECT text FROM all_source WHERE name='MY_PROCEDURE' ORDER BY line

where MY_PROCEDURE is the stored procedure name.

Below is sample output:

Get Data All: "TEXT" "PROCEDURE Usp_Get_Blob
"(
"P_DOC_ID INT,
"P_DOC_TEXT OUT BLOB)
"as
"begin
" select B1 into p_doc_text
" from blobtest
" where ID = p_doc_id;
"end; "

1

I was not able to find stored procedure with any of the methods above.

Reason: stored procedure was inside package. Oracle uses packages to gather several stored procedures in one module.

FrenkyB
  • 6,625
  • 14
  • 67
  • 114
1

This will display the stored procedure and its contents stored in the table.

select
   name c1,
   text c2
from
   dba_source
where
   name = upper('procedure_name')
order by
   line asc;  
sarthakgupta072
  • 451
  • 6
  • 13
0

select * from USER_SOURCE where type='PROCEDURE' and name='my_stored_procedure.'