How to list all the functions in an Oracle DB and order them by their modification date - sql

I would like to obtain a list of all functions in an Oracle database, along with their respective modification dates. Is there an SQL query that could provide me with this information?
I attempted to use the following query:
SELECT * FROM DBA_OBJECTS
WHERE OBJECT_TYPE = 'FUNCTION'
which I found in a Donald Burleson article, but I received an error message stating:
ORA-00942: table or view does not exist.

If you're unable to access the DBA_OBJECTS view, it is likely due to a lack of necessary privileges granted by the DBA, since this view contains metadata for all objects of the entire database.
However, you can utilize the ALL_OBJECTS view instead, which contains metadata only for objects that your user has access to. So, if you are able to access this view, you can make use of its LAST_DDL_TIME column, which provides the timestamp for the last modification of each object, resulting from a DDL statement (including grants and revokes).
That being said, your query should be updated as follows:
SELECT OBJECT_NAME, LAST_DDL_TIME
FROM ALL_OBJECTS
WHERE OBJECT_TYPE = 'FUNCTION'
ORDER BY LAST_DDL_TIME DESC

Related

How to get count of tables present in a particular schema in snowflake?

I am trying to find the count of tables present in CDC schema, I tried with information_schema but the information_schema is empty for me.
The following SQL would give you the required results:
use schema <schemaname>;
show tables;
The result shows the number of tables in the schema.
Note: The information schema in a particular database only displays the objects to which your current role in the session has access.
Please make sure that the role you are using has required access control privileges to view the database objects. If not, if you have access to high privilege roles like SYSADMIN/ACCOUNTADMIN you could use them when viewing metadata information in Snowflake.
You can try querying the INFORMATION_SCHEMA.TABLES View with a query :
select count(distinct TABLE_NAME)
from <database_name>.INFORMATION_SCHEMA.TABLES
where TABLE_CATALOG = <database_name>
and TABLE_SCHEMA = <schema_name>;
Alternatively, you can also use the SHOW TABLES command as follows to get the count.
show tables in schema <DATABASE_NAME>.<SCHEMA_NAME>;
select count(*) from table(result_scan('<last_query_id>'));

Getting Schemas from Oracle DB throwing error

This query
select distinct owner from dba_objects
is throwing this error
ORA-00942: table or view does not exist
Does that make any sense at all?
You have to use an administrative user (such as sys or system). If you do not have access to such a user, you could use the all_objects view instead of dba_obejcts. Any user can query it, and will get results only for the objects it has privileges to.
It does if you don't have select privs on the DBA_OBJECTS view or if you don't have a local or global synonym to the SYS.DBA_OBJECTS view. You could try selecting from SYS.DBA_OBJECTS instead.
As others state, this is a privileges issue.
However, if a user needs access to the more privileged DBA data dictionary objects you should grant them the SELECT_CATALOG_ROLE role. Connecting with SYS or SYSTEM, or even as a DBA, should be discouraged for normal operations.

dbms_metadata.get_ddl not working

I want to get the DDL of Table CARD_TABLE in XT schema
SQL> select dbms_metadata.get_ddl('TABLE','CARD_TABLE','XT') from dual;
ERROR:
ORA-31603: object "CARD_TABLE" of type TABLE not found in
schema "XT"
ORA-06512: at "SYS.DBMS_METADATA", line 5746
ORA-06512: at "SYS.DBMS_METADATA", line 8333
ORA-06512: at line 1
But my select Query works
select count(*) from XT.CARD_TABLE;
count(*)
---------
0
I queried dba_objects it still got the table:
SQL> select owner,object_type from DBA_OBJECTS
where object_name='CARD_TABLE' 2
3 ;
PUBLIC SYNONYM
XT TABLE PARTITION
XT TABLE PARTITION
XT TABLE PARTITION
XT TABLE
XT TABLE PARTITION
VAT TABLE
7 rows selected.
From the dbms_metadata documentation:
If nonprivileged users are granted some form of access to an object in someone else's schema, they will be able to retrieve the grant specification through the Metadata API, but not the object's actual metadata.
So unless you're connected as a privileged user, you can't see the DDL for another user's objects. You would need to connect as SYS, or have the SELECT_CATALOG_ROLE role granted to your user to be able to get XT's object definition.
Even with that role:
In stored procedures, functions, and definers-rights packages, roles (such as SELECT_CATALOG_ROLE) are disabled. Therefore, such a PL/SQL program can only fetch metadata for objects in its own schema. If you want to write a PL/SQL program that fetches metadata for objects in a different schema (based on the invoker's possession of SELECT_CATALOG_ROLE), you must make the program invokers-rights.
If you're calling dbms_metadata from an anonymous PL/SQL block that doesn't matter, but if you're calling it from a procedure you will have to include an AUTHID clause in the procedure declaration, adding AUTHID CURRENT_USER.
grant SELECT_CATALOG_ROLE to <user> with delegate option;
it work for me. Do this after modify procedure
grant SELECT_CATALOG_ROLE to procedure <procedure name>;

PostgreSQL Views Query

I am new in writing queries. I need a list of views that every user can write queries to.
SELECT *
FROM Information_Schema.Views;
I tried that, but I guess it is a list of all views, but I need only those views that all users could write queries to.
The information about views is stored in information_schema.tables:
SELECT *
FROM Information_Schema.Tables
WHERE table_type = 'VIEW';
This will list each each users visible views (without the superusers.)
SELECT
usename, schemaname||'.'|| viewname as view
FROM
pg_views
,pg_user
WHERE
has_table_privilege (
pg_user.usename,
schemaname||'.'|| viewname,
'select'
)
AND
schemaname NOT IN (
'pg_catalog',
'information_schema'
)
AND usesuper=false
As far as listing a view that is visible to all users ... Postgres has fine grained permissions so there is no easy flag to search for. You would have to match that each view would produce a row of every user. But you could create a group for your users and then query for that.
select * from pg_catalog.pg_views
where schemaname NOT IN ('pg_catalog', 'information_schema')
order by schemaname, viewname;
will show you the viewname and its owner
If you are going to use the information schema, you need to be aware how it works. Per documentation:
The view views contains all views defined in the current database.
Only those views are shown that the current user has access to (by way
of being the owner or having some privilege).
Bold emphasis mine. This also provides a handy way of solving your problem. Run your query with a plain, new, non-superuser role that that wasn't granted membership in any other roles nor any direct privileges on any views (yet).
If you want to exclude system views add the WHERE clause:
SELECT *
FROM information_schema.views
WHERE table_schema NOT LIKE ALL ('{pg_%,information_schema}'::text[]);
You get only those views (and all of them), that public can access.
SQL Fiddle.
(Yes, use the query on information_schema.views you already had, no point in using information_schema.tables, like has been suggested.)
For more specific needs, I suggest you use the system catalogs instead. Actual privileges are stored in the system table pg_class in the column relacl. And the view pg_views lists all views, not just the ones the current role has privileges for.
Resolving actual privileges is not trivial. Use the dedicated "Access Privilege Inquiry Functions" like has_table_privilege() that #user17130 already suggested. Related answer:
How to check if a table exists in a given schema

Can't select from dba_tab_cols from within stored procedure (PL/SQL)

I'm trying to SELECT from the dba_tab_cols view from within a stored procedure. It's not working and I don't know why.
If I execute the following SQL as a query:
SELECT t.data_type FROM dba_tab_cols t
WHERE
t.table_name = 'ACCOUNTTYPE' AND
t.column_name = 'ACCESSEDBY';
it works fine. However if I copy it into a stored procedure like so:
SELECT t.data_type INTO dataType FROM dba_tab_cols t
WHERE
t.table_name = 'ACCOUNTTYPE' AND
t.column_name = 'ACCESSEDBY';
I get the error message "PL/SQL: ORA-00942: table or view does not exist" and the editor highlights dba_tab_cols while trying to compile. The same db user is being used in both cases.
dataType is declared as:
dataType varchar2(128);
PL/SQL (Oracle 9)
Anybody know the issue?
It's most likely a priviledges issue. Is the permission to access dba_tab_columns via a role or is it a direct select grant to your user? Priviledges granted via Roles aren't available in SPROCS.
A quick look on google suggests using all_tab_cols instead and seeing if that table has the required info you need.
To add to Eoin's answer:
For most people, it comes as a
surprise that the user cannot select
the table from within a procedure if
he has not been granted the select
right directly (as opposed to through
the role)
If table user tries to compile this
procedure, he gets a ORA-00942
although this table certainly exists
and he was granted the right to select
this table. The problem is that
procedures don't respect roles; only
directly granted rights are respected.
So, that means that table owner has to
regrant the right to select:
http://www.adp-gmbh.ch/ora/err/ora_00942.html
I don't have oracle installed, but perhaps dataType is a reserved word. I'd try something else.