I found a function on DBViewer, but it doesn't present on PL/SQL - oracle

I'm new to PL/SQL and DBView.
I got below log after doing some action on front end application.
When I want to check on PL/SQL function/ procedure/ package, I can't find it using "find database objects" fiture.
Command : { ? = call ap_val_sb_prod_code (?,?,?,?,?,?)
I/P 1 is : 615
I/P 2 is : 9222
I/O 11 is 0
Please help.

You didn't say how you tried to find it.
Generally speaking, if it is your own schema, you'd query USER_OBJECTS, e.g.
select * from user_objects where upper(object_name) = 'AP_VAL_SB_PROD_CODE';
If you have privileges to view other users' objects, then query ALL_OBJECTS instead:
select * from all_objects where upper(object_name) = 'AP_VAL_SB_PROD_CODE';
Finally, to check the whole database, query DBA_OBJECTS (but you have to have privileges to do that):
select * from dba_objects where upper(object_name) = 'AP_VAL_SB_PROD_CODE';

Related

DBA_HIST_ACTIVE_SESS_HISTORY get sql by user and object schema

Hi I am learning ASH and AWR tables but any ideas as to how i can get list of sql, objects and schema owner accessed by a give user in last 30 days ? Basically get all SQL text, and then search within this SQL to see if a given object (table, package, function, view etc ) is accessed for a given schema and by which user ? Any ideas suggestion on where and how to start ?
You could join the following views -
DBA_HIST_ACTIVE_SESS_HISTORY
DBA_USERS
DBA_HIST_SQLTEXT
To filter the history for last 30 days, use sample_time of DBA_HIST_ACTIVE_SESS_HISTORY view.
Something like -
SELECT
h.sample_time,
u.username,
h.program,
h.module,
s.sql_text
FROM
DBA_HIST_ACTIVE_SESS_HISTORY h,
DBA_USERS u,
DBA_HIST_SQLTEXT s
WHERE sample_time >= SYSDATE - 30
AND h.user_id=u.user_id
AND h.sql_id = s.sql_iD
ORDER BY h.sample_time
/
The very best and simplest way to fetch related data using below query.
SELECT H.SAMPLE_TIME,
U.USERNAME,
H.PROGRAM,
H.MODULE,
S.SQL_TEXT,
H.SQL_ID,
H.TOP_LEVEL_SQL_ID,
H.BLOCKING_SESSION_STATUS
FROM DBA_HIST_ACTIVE_SESS_HISTORY H, DBA_USERS U, DBA_HIST_SQLTEXT S
WHERE H.SAMPLE_TIME >= SYSDATE - 30
AND H.SQL_ID = S.SQL_ID
--AND H.PROGRAM IN ('Toad.exe', 'SQL Developer')
--AND U.USERNAME ='YOUR_USERNAME'
ORDER BY H.SAMPLE_TIME DESC
In the above code you can also fetch data based on your requirements as below.
1. Custom user data: Just modify YOUR_USERNAME with your real username.
2. Program: Program name can be anything like SQL Developer or JDBC Thin client to identify from which client the queries are getting triggered, but optional.
Hope it will help and answer to your question. Thanks :)

Oracle select schema depending on select-statement

I have have two schemas with the same table. e.g schema1.adress and schema2.adress.
Both tables are identical.
Layout of table customer:
customerno: integer
name: varchar2(50)
Now I want to get the customers of schema 1 using something like
"select * from customer where schemaname = 1" or
"select * from customer where schemaname = 2"
Is there a mechanism in Oracle that can switch the schema depending on a criteria in a select-statement?
Before you ask: for a new project I have to query legacy schemas. I cannot change the schema, but I can set any permission on the schema / user.
Any ideas?
Thanks for any response,
Sven
You could create a private synonym for the user for the schema.table you want them to access,
create synonym user1.customer for schemaname1.customer;
create synonym user2.customer for schemaname2.customer;
Then your queries would always just be select * from customer;
So you are logged in as the same user both when you want to select from schema1.customer and select from schema2.customer?
A possible way can be something like:
select *
from (
select 'schema1' schema$name, c.* from schema1.customer c
union all
select 'schema2' schema$name, c.* from schema2.customer c
)
where schema$name = 'schema1'
If you can create views, it can be an idea to create a view like:
create or replace view customer_view as
select 'schema1' schema$name, c.* from schema1.customer c
union all
select 'schema2' schema$name, c.* from schema2.customer c
That makes it possible to do:
select *
from customer_view
where schema$name = 'schema1'
Whether you use view or not, Oracle optimizer can in most cases like this push the predicate "where schema$name = 'schema1'" into the UNION ALL and then it recognizes that it does not need to access schema2.customer at all.

Oracle: Explain plan_table query executing order

I'm trying to understand the Oracle plan_table and ran few SQL statements to populate the plan_table...From the statements generated in the plan_table, How can I identify the order in which the statements are executed.
Selecting directly from the PLAN_TABLE is somewhat "deprecated". At least it's absolutely unnecessary nowadays. You can use dbms_xplan to view the execution plan of an explained statement:
explain plan for
select *
from your_table;;
select *
from table(dbms_xplan.display);
More details in the manual: http://docs.oracle.com/cd/E11882_01/appdev.112/e40758/d_xplan.htm#CACICEDJ
The manual also contains an example (hierarchical) SELECT statement to retrieve the contents from the PLAN_TABLE directly:
SELECT id, LPAD(' ',2*(LEVEL-1))||operation operation, options,
object_name, object_alias, qblock_name, position
FROM plan_table
START WITH id = 0 AND statement_id = 'xxxxx'
CONNECT BY PRIOR id = parent_id AND statement_id = 'xxxxx'
ORDER BY id;
The above is taken from: http://docs.oracle.com/cd/E11882_01/server.112/e26088/statements_9010.htm#sthref5965
You need to replace 'xxxx' with the statement_id you are using (which requires a set statement_id in the explain plan statement)

Native method to pull out metadata about packages, procedures and functions?

I wish to be able to query Oracle for a list of public procedures and functions that belong to a package, something along the lines of:
select procedure_name from all_package_procedures where package_name = :my_package_name;
I also wish to be able to query Oracle for a list of parameters for a given procedure or function, something along the lines of:
select parameter_name, in_or_out, parameter_type from all_function_parameters where function_name = :my_function_name;
Is this possible natively? If not, does anyone know of existing code to achieve this?
You can query USER_OBJECTS & USER_PROCEDURES to get a list of all procedures & functions belonging to a particular package
SELECT procedure_name
FROM user_procedures
WHERE object_id = (SELECT object_id
FROM user_objects
WHERE object_name = '<YOUR-PACKAGE-NAME>'
AND object_type = 'PACKAGE')
Replace user_objects & user_procedures with all_objects & all_procedures respectively to fetch packages & procedures owned by other users.
I also wish to be able to query Oracle for a list of parameters for a given procedure or function,
For this, you can query user_arguments or all_arguments to fetch parameters of on object owned by the current user & all users respectively
SELECT argument_name,
data_type
FROM user_arguments
WHERE package_name = '<name-of-your-package-procedure-function>'
My own answer, derived from Sathyas, for the reference of others. Here is a single query to pull out a denormalized result of all procedures and their arguments for a given package:
select p.procedure_name
, a.argument_name
, a.data_type
, a.defaulted
, a.default_value
, a.in_out
, a.position
from all_procedures p
inner join all_objects o
on o.object_id = p.object_id
inner join all_arguments a
on a.package_name = o.object_name
and a.object_name = p.procedure_name
where o.object_type = 'PACKAGE'
and o.object_name = 'PACKAGE_NAME'
order by p.procedure_name, a.position;

Oracle: Get list of functions for user

How do I get a list of all the functions for a particular user?
EDIT for question clarification:
When (as USER1) I run
select * from all_objects
where owner = 'USER2'
and object_type = 'FUNCTION';
it doesn't return all the functions that I know USER2 owns. I suspect that it is only returning those functions that USER1 is allowed to view/execute.
Is that suspicion correct?
Also, if that is true, is there a way to get around this?
Yes, your suspicion is correct. The ALL_OBJECTS view will only list those items that the current user has access to.
If you can log in as USER2, then you can query USER_OBJECTS as that user to see all objects owned by that user.
If you can log in as SYSTEM, then you would have access to all objects regardless of owner, so the list provided by ALL_OBJECTS (or DBA_OBJECTS) would be complete.
If you can't log in as a user that has access to all of USER2's objects, then you can't list all of USER2's objects.
If you mean a list of functions the belong to a particular user then:
select object_name
from all_objects
where owner = 'WHOEVER'
and object_type = 'FUNCTION';
This will return only stand-alone functions, not procedures or function in packages, that belong to the schema 'WHOEVER'.
To obtain a list of all functions that the current user can access:
select object_name
from all_objects
where object_type = 'FUNCTION';
select * from dba_objects
where owner = 'USER2'
and object_type = 'FUNCTION';

Resources