Oracle: Get list of functions for user - oracle

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

Related

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

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

how to find running procedure name

help to solve issue related session monitoring.
select sql_id,
(select o.owner || '.' || o.OBJECT_NAME
from dba_objects o
where o.OBJECT_ID = cu) object,
'LINE' ||'-' || program_line# line
from v$sql
Typically (if your user has relevant authority and you run a client like sqldeveloper, for example) you can do a session monitor by going to Tools > Session Monitor to view all open sessions.
Otherwise, you could use this very basic i have used many, many times to find open sessions in your DB running by the user you're logged into (if this is relevant);
select
oracle_username,
os_user_name,
locked_mode,
object_name,
object_type,
sysdate,
a.session_id,
a.process,
c.machine,
c.program,
c.module,
c.action,
c.logon_time,
c.last_call_et,
c.state
from
v$locked_object a,dba_objects b, v$session c
where
a.object_id = b.object_id
and C.sid = a.SESSION_ID
and ORACLE_USERNAME = user
order by OBJECT_NAME, ORACLE_USERNAME;
This SQL can be adjusted to your needs. Just remember that you can't view sessions if your user doesn't have the right permissions.
this is the best I could gather from your question, can you be more specific...?

Checking for specific permissions of Oracle users

Problem: I am working on a query that will produce a list of all Oracle users. I wish to determine in the query if they have the specific grant permissions for CONNECT and APPUSER and show them in a single table.
What I have tried: I am using one table, DBA_ROLE_PRIVS. This table shows all the information I need, but am failing to query it correctly. I can show all users who have permission to Connect with:
SELECT GRANTEE as "User Name", granted_role as "Connect"
FROM DBA_ROLE_PRIVS
WHERE GRANTED_ROLE='CONNECT';
I can also show users who have permission to APPUSER, simply by replacing CONNECT with APPUSER.
My problem is showing both permissions in one query. I have tried using different JOINs. However, using that seems to require two tables or more. I have researched a "self-join", but do not understand how to use two WHERE clauses. I have tried things like:
SELECT grantee as "User Name", t1.granted_role as "Connect", t2.granted_role as "APPUSER"
FROM t1.DBA_ROLE_PRIVS join t2.DBA_ROLE_PRIVS on t1.GRANTEE = t2.GRANTEE
WHERE t1.GRANTED_ROLE='CONNECT' and t2.GRANTED_ROLE='APP_USER';
I want my final query to show something like:
User Name Connect App User
---------- ---------- ----------
Bob CONNECT APPUSER
Sue APPUSER
Nick CONNECT APPUSER
Rob CONNECT
SELECT GRANTEE as "User Name", granted_role from DBA_ROLE_PRIVS where GRANTED_ROLE in ('CONNECT','APPUSER');
if you need one row for each user and two column for each access, you can use this
select c.GRANTEE as "User Name", a.granted_role as "Connect", c.granted_role as "APPUSER"
FROM
(SELECT GRANTEE, granted_role from DBA_ROLE_PRIVS where GRANTED_ROLE = 'CONNECT') a,
FULL OUTER JOIN
(SELECT GRANTEE, granted_role from DBA_ROLE_PRIVS where GRANTED_ROLE = 'APPUSER') c
on a.GRANTEE = c.GRANTEE;

Finding sequences and triggers associated with an Oracle table

I have used this query to fetch the list of sequences belonging to an Oracle database user:
SELECT * FROM all_sequences x,all_tables B
WHERE x.sequence_owner=B.owner AND B.TABLE_NAME='my_table';
But that database user is having many more sequence also, so the query returns me all the sequence of the database user. Can anybody help me to find the particular sequence of my_table using query so that I can get the auto increment id in my application.
i want the query which fetch list of table of my database user with the sequence and triggers used in the table
You can get the triggers associated with your tables from the user_triggers view. You can then look for any dependencies recorded for those triggers in user_dependencies, which may include objects other than sequences (packages etc.), so joining those dependencies to the user_sequences view will only show you the ones you are interested in.
Something like this, assuming you are looking at your own schema, and you're only interesting in triggers that references sequences (which aren't necessarily doing 'auto increment', but are likely to be):
select tabs.table_name,
trigs.trigger_name,
seqs.sequence_name
from user_tables tabs
join user_triggers trigs
on trigs.table_name = tabs.table_name
join user_dependencies deps
on deps.name = trigs.trigger_name
join user_sequences seqs
on seqs.sequence_name = deps.referenced_name;
SQL Fiddle demo.
If you're actually looking at a different schema then you'll need to use all_tables etc. and filter and join on the owner column for the user you're looking for. And if you want to include tables which don't have triggers, or triggers which don't refer to sequences, you can use outer joins.
Version looking for a different schema, though this assumes you have the privs necessary to access the data dictionary information - that the tables etc. are visible to you, which they may not be:
select tabs.table_name,
trigs.trigger_name,
seqs.sequence_name
from all_tables tabs
join all_triggers trigs
on trigs.table_owner = tabs.owner
and trigs.table_name = tabs.table_name
join all_dependencies deps
on deps.owner = trigs.owner
and deps.name = trigs.trigger_name
join all_sequences seqs
on seqs.sequence_owner = deps.referenced_owner
and seqs.sequence_name = deps.referenced_name
where tabs.owner = '<owner>';
If that can't see them then you might need to look at the DBA views, again if you have sufficient privs:
select tabs.table_name,
trigs.trigger_name,
seqs.sequence_name
from dba_tables tabs
join dba_triggers trigs
on trigs.table_owner = tabs.owner
and trigs.table_name = tabs.table_name
join dba_dependencies deps
on deps.owner = trigs.owner
and deps.name = trigs.trigger_name
join dba_sequences seqs
on seqs.sequence_owner = deps.referenced_owner
and seqs.sequence_name = deps.referenced_name
where tabs.owner = '<owner>';
One way would be to run these queries to check if there are any sequence's Pseudocolumns (NEXTVAL and CURRVAL ) used in your functions , procedures, packages, Triggers or PL/SQL JAVA SOURCE.
select * from user_source where
UPPER(TEXT) LIKE '%NEXTVAL%';
select * from all_source where
UPPER(TEXT) LIKE '%NEXTVAL%';
Then go to the specific Procedure, Function or Trigger to check which column/table gets populated by a sequence.
The query could also be used with '%CURRVAL%'
This might not help if you are running inserts from JDBC or other external applications using a sequence.
Oracle 12c introduced the IDENTITY columns, using which you could create a table with an identity column, which is generated by default.
CREATE TABLE t1 (c1 NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY,
c2 VARCHAR2(10));
This will internally create a sequence that auto-generates the value for the table's column.So, If you would like to know which sequence generates the value for which table, you may query the all_tab_columns
SELECT data_default AS sequence_val
,table_name
,column_name
FROM all_tab_columns
WHERE OWNER = 'HR'
AND identity_column = 'YES';
SEQUENCE_VAL |TABLE_NAME |COLUMN_NAME
-----------------------------------------|-------------------------------------
"HR"."ISEQ$$_78160".nextval |T1 |C1
I found a solution to this problem to guess the sequence of a particular sequence
select * from SYS.ALL_SEQUENCES where SEQUENCE_OWNER='OWNER_NAME' and LAST_NUMBER between (select max(FIELD_NAME) from TABLE_NAME) and (select max(FIELD_NAME)+40 from TABLE_NAME);
This query will guess by search the LAST_NUMBER of the sequence value between MAX value of the field using sequence and Max value + 40 (in my case cache value is 20, so I put 40)
select SEQUENCE_NAME from sys.ALL_TAB_IDENTITY_COLS where owner = 'SCHEMA_NAME' and table_name = 'TABLE_NAME';

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;

Resources