How to check if a stored procedure exist? - oracle

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.

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'

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'

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.

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; "

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.

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;

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

Related

Oracle: What SQL for "show error" command?

When compiling a package or stored procedure in SQL*Plus, show errors is used to display any errors from the last compilation.
What is the equivalent SQL query to fetch this information?
You want to look at one of the _ERRORS views - either USER_ERRORS, ALL_ERRORS, or DBA_ERRORS. For example:
SELECT *
FROM DBA_ERRORS
WHERE OWNER = 'YOUR_SCHEMA' AND
NAME = 'WHATEVER'
ORDER BY SEQUENCE
Docs here
Share and enjoy.

Is it possible to get the stored procedure name by having line number in PL\SQL?

I'm just writing a tool to automate a process. Out of it, I just want it to do fetch the stored procedure name if I give the line number of a package of many procs.
Is it possible to do so, or is there some other way to get the procedure name with line number or "specific text"?
Have a look at all_procedures
Maybe you can figure it out by using the column subprogram_id.
It seems like, this column identifies the procedures as they are defined in the package header.
Try to select from the SYS view ALL_SOURCE querying by the owner, package name and line number or owner package name and text.
Below are two examples that assume that you will replace MYOWNER,MYPACKAGE, Myprocedure and MYLINENUMBER with your own information.
select * from ALL_SOURCE
where OWNER = 'MYOWNER' and
TYPE='PACKAGE' and
NAME='MYPACKAGE' and
TEXT like 'PROCEDURE Myprocedure%'
select * from ALL_SOURCE
where OWNER = 'MYOWNER' and
TYPE='PACKAGE' and
NAME='MYPACKAGE' and
LINE = MYLINENUMBER

How to get information about a User-Defined Type?

In simplicity, PL/SQL generally follow the following:
DECLARE
Variable declaration
BEGIN
Program Execution
EXCEPTION
Exception handling
END;
I am quite new to PL/SQL and i am looking at the variable declaration section where i would like to find out more information on SALES_PRODUCT_TY_LIST.
Is there a table i may look up to check on information on SALES_PRODUCT_TY_LIST, such as checking out table column information from all_tab_cols view?
CREATE OR REPLACE PROCEDURE GET_DISCOUNTS
(
v_have_list SALES_PRODUCT_TY_LIST
)
IS
QUERY VARCHAR(5000);
...
Thanks.
The Oracle database has an extensive data dictionary (what some other DBMS products call the INFORMATION SCHEMA). You can find all the views here. Alas, the revised ToC structure makes it harder to find something in the 11g documentation unless you already know what you're looking for, so use the index instead. 8-)
Anyway, the views you need to query are ALL_TYPES and ALL_TYPE_ATTRS.
This seems to be user defined collection type. You can find some information about it querying all_types/user_types view:
select * from user_types where type_name = 'SALES_PRODUCT_TY_LIST'
The definition of the type can be found for example by querying all_source/user_source view:
select text from user_source where name = 'SALES_PRODUCT_TY_LIST' order by line
Try this to get DDL:
SELECT dbms_log.substr(dbms_metadata.get_ddl('TYPE', 'SALES_PRODUCT_TY_LIST'), 32767,1)
FROM DUAL;
see: http://www.myoracleguide.com/s/gen_schema.htm
Ok i found something:
select *
from all_objects
where object_name like 'SALES%';

Obtain stored procedure metadata for a procedure within an Oracle package using ADO.NET

I am trying to obtain the stored procedure metadata (procedure name,parameter types,parameter names etc) for a procedure declared within an Oracle package, using the standard ADO.NET API - DbConnection.GetSchema call. I am using the ODP driver.
I see that the Package is listed in the 'Packages' and 'PackageBodies' metadata collections. The procedure parameter appears in the 'Arguments' and 'ProcedureParameters' collections. I do not see a way to get to the procedure information via the package metadata. Even if the procedure does not have any parameters there is a row in the 'ProcedureParameters' collection for this procedure.
My question: To obtain the procedure metadata do I have to query the 'ProcedureParameters' collection and search for an entry with the required package name? I can then construct the procedure metadata based on the parameter information. Is there a shorter or quicker way to obtain the same information?
I'm not sure how you'd get this using ADO.NET, but you can directly query the database to get this information as follows:
SELECT *
FROM SYS.DBA_PROCEDURES
WHERE OBJECT_TYPE = 'PACKAGE' AND
OBJECT_NAME = '<your package name here>' AND
PROCEDURE_NAME IS NOT NULL;
Once you've run the above query you'll have a result set which has, among other things, the PROCEDURE_NAME. Given the package name and the PROCEDURE_NAME, you can find parameter info using the following query:
SELECT *
FROM SYS.ALL_ARGUMENTS
WHERE PACKAGE_NAME = '<your package name here>' AND
OBJECT_NAME = '<PROCEDURE_NAME from query above>';
Share and enjoy.
With help from Bob I've used the following query to obtain a list of stored procedures defined within a package.
SELECT a.OBJECT_NAME,p.PROCEDURE_NAME FROM SYS.ALL_OBJECTS a, SYS.ALL_PROCEDURES p WHERE a.OBJECT_NAME = p.OBJECT_NAME AND a.OBJECT_TYPE = 'PACKAGE' AND a.OWNER = '" + ownerName + "' AND p.PROCEDURE_NAME IS NOT NULL"
This returns all stored procedures for a particular user. I can then use the 'ProcedureParameters' collection to obtain the parameter information for them.
NOTE: Do not query the SYS.DBA_PROCEDURES table. The user credentials you use to execute the query might not have 'select' privileges on that table.

question about pl/sql stored program text

I use TOAD to do my PL/SQL development. In TOAD when i type a procedure name and press f4, I can see this procedure's source code. I think TOAD get the source code from v$sqltext view. To confirm my thought, I wrote a query:
select * from v$sqltext
but when I execute the upper query, Oracle give me an error:
ORA-00942: table or view does not
exist
00942. 00000 - "table or view does not exist"
*Cause:
*Action: Error at Line: 29 Column: 15
So I think TOAD get the procedure's source from other place instead of v$sqltext view. Anyone can tell me about this? Great thanks.
The full query for a stored procedure (not in a package):
select text
from all_source
where owner = 'MYSCHEMA'
and type = 'PROCEDURE'
and name = 'MY_PROCEDURE'
order by line;
If you are connected as user MYSCHEMA than you can use USER_SOURCE:
select text
from user_source
where type = 'PROCEDURE'
and name = 'MY_PROCEDURE'
order by line;
Other values for TYPE are:
TYPE BODY
FUNCTION
TRIGGER
TYPE
JAVA SOURCE
PACKAGE BODY
PACKAGE
select * from all_source
See Database Reference for ALL_SOURCE and V$SQLTEXT.
If you have select priv on DBA* tables, then do check out select * from dba_source. This table will have the entire source code.

Resources