How to know whether the created view is valid or not - oracle

I am new to db.
Lets say I have created a view with force.
So how can I get know whether the view created is invalid or not?
I mean are there any queries from which I can get to know the validity status of the view?
Thanks.

You can use Oracle dictionary view user_objects:
select object_name, status from user_objects
where object_type = 'VIEW' and object_name = 'YOUR_VIEW';
If the table on which the view is based is altered for any reason, you may have to recompile the view. For example, if a table’s structure is altered, such as by a change to a column’s datatype, or perhaps if a column is dropped from the table altogether - a column that is used
by the view — then it may change the status of the view to 'INVALID'.
Also note that there are there versions of "all objects" view:
USER_OBJECTS - all objects owned by a current user;
ALL_OBJECTS - all objects on which a current user has any privileges;
DBA_OBJECTS - all database objects (you would need special privileges to access this view)
ALL_ and DBA_ versions have an additional column OWNER containing the owner of the object.
These "naming rules" are applied to the different Oracle dictionary views: [USER_ | ALL_ | DBA_] [INDEXES | TABLES | VIEWS | etc]

Maybe this helps:
SELECT object_name,
status
FROM user_objects
WHERE object_type = 'VIEW';
or (for views in all schemas)
SELECT owner,
object_name,
status
FROM all_objects
WHERE object_type = 'VIEW';

Apart from checking the status about that new view in the dictionary, you can do:
select 1 from OWNER.MY_NEWLY_CREATED_VIEW;
or
select count(*) from OWNER.MY_NEWLY_CREATED_VIEW;
Whatever the statement of your new view, from that feedback you know it works or not.

Oracle docs include the following as a way to update or find out if a view is invalid:
Recompile the view e.g. customer_ro via the following statement:
ALTER VIEW customer_ro
COMPILE;
If Oracle Database encounters no compilation errors while recompiling customer_ro, then customer_ro becomes valid. If recompiling results in compilation errors, then the database returns an error and customer_ro remains invalid.

Related

How can I get all the objects connected to a table

I have to drop a table and to create a view with the same fields and name.
How can I get previously all the objects (tables, triggers, procedure, ...) connected to the original table so as to eventually or recompiling either modifying them?
Oracle version 11g
Thanks!
Run such a query; currently, I'm connected as a privileged user who is capable of querying DBA_DEPENDENCIES.
SQL> SELECT owner, name, type
2 FROM dba_dependencies
3 WHERE 1 = 1
4 AND referenced_owner = 'SCOTT'
5 AND referenced_type = 'TABLE'
6 AND referenced_name = 'EMP';
OWNER NAME TYPE
------------------------------ ------------------------------ ------------------
SCOTT TRG_BIU_EMP TRIGGER
MIKE PKG_EMPLOYEE PACKAGE BODY
SQL>
It says that emp table, owned by scott, is referenced by two other objects:
trigger named trg_biu_emp owned by scott
package body named pkg_employee owned by mike
You can also go with all_dependencies and user_dependencies (pay attention to their description! owner column is missing in user_ views), but you'll get fewer and fewer results because both of them contain less information than dba_dependencies.
Therefore, if you don't want to miss something, look everywhere (i.e. dba_ views). If you don't have required privileges, talk to your DBA.
Also, note that such a query won't discover possible references to that object elsewhere, such as front-end applications developed in e.g. Oracle Forms and Reports or Apex or ...

Get owner of the current view in Oracle

I want to create a view that containt a column which refers to the owner of this view. Something like that:
create or replace view scott.owner_v
as
select something() owner from dual;
Note: something() shouldn't necessary be a function or package reference. It can be anything that gives a desired output.
So querying select owner from scott.owner_v under JEREMY user, for example, would return SCOTT and when I compile such view in HR schema I get HR in owner column.
Maybe seems dumb to query SCOTT.owner_v to get SCOTT but I need it in terms of building DWH referring to different sources which are situated in different schemas. So then I would build dynamically a new view which is on a "higher" level that collects data from all schemas with extra column like owner which shows a source of data. I can put this column when building this "higher" view but I want to keep it as simple as it can be.
Obviously, I tried to place into a view the following parameters
sys_context('USERENV','CURRENT_USER')
sys_context('USERENV','CURRENT_SCHEMA')
user
but it refers to current logged user not to owner of the view.
Any help appreciated.
Just create local functions which returns own schemas in all schemas where do you want to create views:
create or replace function local_obj_owner return varchar2 as
begin
return $$PLSQL_UNIT_OWNER;
end;
/
Then add it into your views:
create view test_view as
select
local_obj_owner as view_owner,
dummy
from dual;
Try
select owner from all_views where view_name = 'OWNER_V';
and/or some alternatives (USER_VIEWS, DBA_VIEWS, ALL_OBJECS, ...).

How to get parameters properties from a stored procedure in Oracle?

I am developing an admin panel where we can determine which stored procedures and views can be called in Oracle 12c schemas, from services in our micro-services platform.
The services must know which parameters a procedure needs, and which columns a view has available, in order to call them.
I the admin panel, the creator of the procedure or view may register it, by typing in these information, like object's name, each parameter's name, length and data type.
But it would be much more elegant if the user just types the name of the object and then a SQL SELECT would retrieve a procedure's parameters properties in a table, and the same with the view's column's properties, so the panel would register those configurations automatically.
Could anybody post a query on how to achieve this? I am very new to Oracle and I don't know how to query the objects metadata.
I believe your are looking for something like this.
SELECT *
FROM SYS.DBA_PROCEDURES
WHERE OBJECT_TYPE = 'PROCEDURE'
AND OBJECT_NAME = 'xxxx'
Once you have that, you can get the parameters from something like this.
SELECT *
FROM SYS.ALL_ARGUMENTS
where object_name = 'procedure_name';
This can be used for views (I used USER_... metadata views, but you can use DBA_... depending of user grants):
SELECT A.TABLE_NAME, A.COLUMN_NAME, A.DATA_TYPE
FROM USER_TAB_COLUMNS A
INNER JOIN USER_VIEWS B ON A.TABLE_NAME = B.VIEW_NAME
/* WHERE A.TABLE_NAME = 'xxxx' */
ORDER BY TABLE_NAME, COLUMN_ID;

How can I find the invisible table in oracle 11g?

There is a table named IM_RESULT_INFO. Because I can see it by SELECT * FROM IM_RESULT_INFO.
But it doesn't exist in table nor view lists in sqldeveloper. I also tested SELECT * FROM all_all_tables and SELECT * FROM dba_tables, and couldn't find the table.
In Eclipse IDE, I searched for it in whole project files, but the only code I found was SELECT ... FROM IM_RESULT_INFO.
I think it is a mixture of tables, but there's no way to analyze it. How can I find it?
It must be a synonym or a view, check the synonyms view to see what object is referenced by it:
SELECT *
FROM all_synonyms
WHERE synonym_name = 'IM_RESULT_INFO'
Or the views view:
SELECT *
FROM all_views
WHERE view_name = 'IM_RESULT_INFO'
You can check in ALL_OBJECTS if you are not sure about the type of an object. It will provide you the Types and other important details also.
For Materialized view please check ALL_MVIEWS.
SELECT *
FROM ALL_OBJECTS
WHERE OBJECT_NAME='IM_RESULT_INFO';

How to check if a trigger is invalid?

I'm working on databases that have moving tables auto-generated by some obscure tools. By the way, we have to track information changes in the table via some triggers. And, of course, it occurs that some changes in the table structure broke some triggers, by removing a column or changing its type, for example.
So, the question is: Is there a way to query the Oracle metadata to check is some triggers are broken, in order to send a report to the support team?
The user_triggers give all the triggers and tells if they are enable or not, but does not indicate if they are still valid.
SELECT *
FROM ALL_OBJECTS
WHERE OBJECT_NAME = trigger_name
AND OBJECT_TYPE = 'TRIGGER'
AND STATUS <> 'VALID'
Have a look at SYS.OBJ$, specifically the STATUS column.

Resources