How to create a comment to an oracle database view - oracle

I would really like to create a comment to a view with a short description of its purpose. Unfortunately it is not possible to create comments to views in oracle. This feature is only for tables, columns and materialized views available. I would like to know how you do describe your database views?

Try:
comment on table <name> is 'text';
The command works on views. For example:
CREATE OR REPLACE VIEW MY_VW AS
SELECT 1 FROM DUAL;
COMMENT ON TABLE MY_VW IS 'Clever comment.';

Its possible to create a comment on VIEWs too :
COMMENT ON TABLE view_name ;

Simply use :
Comment on table 'view Name' is ' Comment ...';
If use view in place of SQL , Oracle throws error invalid object category for COMMENT command".
Materialized View : we can simply comment using:
Command on Materialized view "View name" is 'Comment ...';

Related

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, ...).

Oracle: why I can't select from VIEW using <SCHEMA_NAME>.<VIEW_NAME>?

There are some changes on the core product on which I'm working and some tables become now views and they are not working anymore because a view cannot be referenced with the schema name in front.
For example, the below will return an error: ORA-00942: table or view does not exist
select * from my_schema.my_view;
while a direct select from the view works fine
select * from my_view;
In case of a table, both scenarios above are working fine, is just the view that doesn't accept schema_name in front.
Why is that? Are there any decent workarounds?
EDIT: the selects are executed with my_schema user
Thanks all for your help, especially #mathguy.
Basically the problem was that my_view was in fact a public synonym for my_view_r which was the actual view and being public, you cannot call it using the schema name in front like I was trying. eg:
select * from my_schema.my_view;
Maybe it will be helpful for others that are facing this issue in the future, the workaround would be to create a private synonym to the same view (my_view_r) using the schema name like below:
create synonym my_schema.my_view for my_view_r;
This is the only way to call a synonym using the schema name.
It's a grant issue.
grant all on my_view to my_schema
Make sure your schema really is the owner by running:
Select * from all_objects where object_name = 'my_view';
I have a view stvytro with owner baninst1. There is a public synonym of the same name. The following both work:
select * from STVYTRO;
select * from baninst1.stvytro;

Unable to alter a view: ORA-00942: table or view does not exist

I want to change FND_LOOKUPS by adding a new column and the SQL statement is :
alter table FND_LOOKUPS add Tag VARCHAR2(150 CHAR);
But the error :
" ORA-00942: table or view does not exist"
always shows.
While FND_LOOKUPS does exist.
I checked the duplicate issues and grant the access by:
GRANT ALL ON FND_LOOKUPS TO public;
And it prompts grant successfully, but the issue still exists.
What's the possible reason for this issue?
Any suggestions would be appreciated, thanks in advance!
Sorry for misleading you guys. I use the following statement to check the "table" and found the object type is a VIEW:
SELECT * FROM all_objects WHERE object_type IN ('TABLE','VIEW') AND object_name = 'FND_LOOKUPS'
So the issue should be how to alter a view by adding one column to it.
I use the following syntax to achieve this:
CREATE OR REPLACE <optional: FORCE EDITIONABLE> VIEW "user_name"."view_name" (all coloumns) AS
<select statement>
FROM
<basic table>
WHERE
<whereclause>
It works fine at last.
Thanks very much to you.

Oracle SQL Developer - How to generate script in correct order of dependencies

I would want to generate create script that will create all the database views which are very many.
I know how to do it in Oracle SQL Developer already using menu: Tools > Database Exports. However I encounter issues doing the export create views scripts. Below are the issues.
Issue #1 The order of creation of view is not correct. In example below, the MY_VIEW_B is created first in the script before its dependency MY_VIEW_A. How can I generate script that are in correct order of dependencies?
CREATE OR REPLACE MY_VIEW_B ("COLUMN1", "COLUMN2") AS
SELECT "COLUMN1", "COLUMN2"
FROM MY_VIEW_A;
CREATE OR REPLACE MY_VIEW_A ("COLUMN1", "COLUMN2") AS
SELECT "COLUMN1", "COLUMN2"
FROM TABLE_A;
Issue #2 The semicolon ";" is carried over to the last line of the view codes but the problem is it is comment line. So this will get error when I execute the script because there is no closing ";" semicolon since it was moved to the line where the comment is. How can I generate script so that the last line of my view code which is ";" semicolon is not auto carried over to second to the last line of the view code which is a comment?
CREATE OR REPLACE MY_VIEW_C ("COLUMN1", "COLUMN2") AS
SELECT "COLUMN1", "COLUMN2"
FROM TABLE_B
--THIS IS A COMMENT;
CREATE OR REPLACE MY_VIEW_C ("COLUMN1", "COLUMN2") AS
SELECT "COLUMN1", "COLUMN2"
FROM TABLE_C;
Thank you.
As Oracle States here:
FORCE
Specify FORCE if you want to create the view regardless of whether the
base tables of the view or the referenced object types exist or the
owner of the schema containing the view has privileges on them. These
conditions must be true before any SELECT, INSERT, UPDATE, or DELETE
statements can be issued against the view.
If the view definition contains any constraints, CREATE VIEW ... FORCE
will fail if the base table does not exist or the referenced object
type does not exist. CREATE VIEW ... FORCE will also fail if the view
definition names a constraint that does not exist.
also check Terminator and Pretty Print to deal with the second issue;
Tested with Oracle SQL Developer Version 4.1.3.20 Build MAIN-20.78

How to add column in a view using ALTER VIEW in sql Oracle 12c [duplicate]

This question already has an answer here:
How to delete a column from a view
(1 answer)
Closed 5 years ago.
INITIAL VIEW CREATED
create or replace view concert_view
as
select concert.concert_id
from concert, event
where concert.concert_id=event.concert_id;
WHEN I TRIED TO ADD COLUMNS USING
alter view concert_view as
select
cname,edate
from concert,event
where concert.concert_id=event.concert_id;
I HAVE AN ERROR MESSAGE
alter view concert_view as
*
ERROR at line 1:
ORA-00922: missing or invalid option
ALTER VIEW is not used in this way. The only options for altering a view are to add/drop/modify constraints or to RECOMPILE the view.
If you want to add columns then just run the CREATE OR REPLACE VIEW statement again with a different select.
A View is basically only a SELECT -statement. If you want to add another column to your view, just change the statement on which it is based.
And recreate a view.

Resources