question about pl/sql stored program text - oracle

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.

Related

Oracle.ManagedDataAccess.Client ORA-00903 invalid table name

I'm using the sqldeveloper and have one database connection with the following connection string:
MES#//localhost:1521/xepdb1
MES is the schema owner and a select statement like this shows me what I want to see:
select count(*) from site
I'm also using Visual Studio and I'm trying to connect to the database by using the Oracle.ManagedDataAccess.Client
I'm using exactly the same connection string.
The connect to the database works fine. But I'm always getting the 00903 error.
Any idea what the problem can be ?
Many thanks in advance
I've tried also something like this:
select count(*) from mes.site
If you have used quoted identifiers to create the table then you will need to use quoted identifiers (and use the same case) whenever you access the table.
For example:
CREATE TABLE MES."site" (something NUMBER);
Then you would need to use:
SELECT count(*) FROM MES."site";
or
SELECT count(*) FROM mes."site";
or
SELECT count(*) FROM mEs."site";
The MES schema name is unquoted so you can use any case (and Oracle will implicitly convert it to upper-case to look it up in the data dictionary); however, "site" is a quoted identifier and Oracle will respect the case-sensitivity of the identifier and you MUST use the correct case and the surrounding quotes.
You can see the exact case you need to use in the result of the query:
SELECT owner, table_name FROM all_tables WHERE UPPER(table_name) = 'SITE';
If the data dictionary shows that the table name is upper-case then you can use an unquoted identifier (assuming that all the other naming rules have been respected and the table name is not a reserved/keyword) otherwise you will need to use a quoted identifier.
Normally you would get the ORA-00942: table or view does not exist exception but you can get ORA-00903: invalid table name when you use a keyword for a table name:
CREATE TABLE "NUMBER" (x) AS
SELECT 1 FROM DUAL;
Then:
SELECT COUNT(*) FROM NUMBER;
Give the exception:
ORA-00903: invalid table name
And:
SELECT COUNT(*) FROM "NUMBER";
Works.
However, MES and SITE are not keywords that should trigger that.
fiddle

Oracle query `select some_name(?) from dual`. What is `some_name` and how to view the source?

I am looking at an already existing code base which connects to an Oracle database. It contains the query that looks like select some_name(?) from dual. What is some_name here? Is it a stored procedure? How can I view the source code for that function?
if you have SQLDeveloper, you can view the source code of SOME_NAME function in the function section, or TOAD, you can find it in the Schema Browser,
or you can query it using below select statement,
SELECT *
FROM all_source
WHERE name = 'SOME_NAME'
ORDER BY line;

How can I avoid errors with dbms_metadata.GET_DEPENDENT_DDL

I'm writing a custom program to dump the database metadata to files in order to manage them with version control. The default way that data pump or export works isn't ideal for a few reasons (eg. I'd like a separate directory per table).
Sql Developer provides a number of ways of creating export scripts for any object. One way is just by right-clicking the object and selecting Quick DDL. By viewing the logs it creates, one can see the actual SQL it issues to create the DDL script. I've used these scripts to write my custom program and for the most part, they've been perfect.
When I generate the DDL for a materialized view, the SQL it generates is:
SELECT DBMS_METADATA.GET_DDL('MATERIALIZED_VIEW',:name,:owner) FROM DUAL
UNION ALL
SELECT DBMS_METADATA.GET_DEPENDENT_DDL('INDEX',TABLE_NAME, TABLE_OWNER) FROM (
SELECT table_name, table_owner FROM all_indexes
WHERE table_owner = :owner AND table_name = :name
AND index_name NOT IN (
SELECT constraint_name FROM sys.all_constraints
WHERE table_name = table_name AND constraint_type = 'P'
) AND ROWNUM = 1
)
UNION ALL
SELECT dbms_metadata.GET_DEPENDENT_DDL ('COMMENT', :name,:owner ) FROM DUAL
For this script, when executed via SQL Developer Quick DDL, it generates the metadata for the materialized view properly. When I run this script in a program (or even manually with SQL Developer itself), it produces the following errors:
ORA-31608: specified object of type COMMENT not found
ORA-06512: at "SYS.DBMS_METADATA", line 5805
ORA-06512: at "SYS.DBMS_METADATA", line 8436
ORA-06512: at line 1
31608. 00000 - "specified object of type %s not found"
*Cause: The specified object was not found in the database.
*Action: Correct the object specification and try the call again.
This particular materialized view doesn't have any comments (obviously), but I would have expected this part of the clause to just return 0 rows instead of generating an error (especially since SQL Developer uses this itself seemingly without errors).
Is there a way I can avoid this error, while still including comments in the metadata if they exist?
This issue exists on both Oracle 10g & 11g databases.
I was not able to test as I don't really know how to create mview without comment... However the below should work for you. Try to query dba_mview_comments instead of dual to not execute the function when you don't have comments.
UNION ALL
SELECT dbms_metadata.get_dependent_ddl ('COMMENT', :name, :owner)
FROM dba_mview_comments mvc
WHERE mvc.mview_name = :name AND
mvc.owner = :owner AND
length(comments) > 0 AND
rownum = 1

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 check if a stored procedure exist?

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.'

Resources