How to use pl sql definitions in with clause while creating mview?
An explanation with example would really help.
Also I have tried the following queries and I have faced some errors.
create table tab1
(
fname varchar2(15) ,
lname varchar2(15)
);
create or replace type user_data_type as object (val1 varchar2(20),val2 varchar2(20));
/
CREATE OR REPLACE Function get_fname_lname(n in varchar2)
RETURN user_data_type as
name user_data_type;
cursor buffer_cur is
select fname,lname from tab1 where fname=n;
BEGIN
name := new user_data_type(null,null);
OPEN buffer_cur;
fetch buffer_cur into name.val1,name.val2;
close buffer_cur;
return name;
End;
/
select * from tab1;
select get_fname_lname(fname).val1 fname,get_fname_lname(fname).val2 from tab1;
drop materialized view mv1;
create materialized view mv1
with names as
(select get_fname_lname(fname).val1 fname,get_fname_lname(fname).val2 lname from tab1)
select fname,lname from names;
select * from mv1;
I have seen this error:
Error while creating mview:
ORA-00905: missing keyword
Can some one point me out what might be the issue.
You can try something like this while creating view , since recursive views are not supported in oracle.
By using with clause you are already creating an inline view and for that view you are trying to create a materialised view which is not supported in oracle
create materialized view mv1
as
select fname,lname from
(select get_fname_lname(fname).val1 fname,get_fname_lname(fname).val2 lname
from tab1);
Related
I have been trying to create a new table by using below query :
"Create table d1_details_test2
as
select * from d1_details"
this above query gives me an error :
actually "d1_details" table has one column which has "Long" datatype and i cannot change it.
so i want to know the any other way to create the table.
Thanks
The long data type is subject to many restrictions. Create table as select is one of these.
You can get around it by applying to_lob in the select, which converts it to a clob:
create table views as
select view_name, text from user_views;
ORA-00997: illegal use of LONG datatype
create table views as
select view_name, to_lob ( text ) lob
from user_views;
desc views
Name Null? Type
VIEW_NAME VARCHAR2(128)
LOB CLOB
ESU_1 is the Source table
create table ESU_1
(
emp_id NUMBER(10),
emp_name VARCHAR2(100)
);
I created a table ESU_2 by using ESU_1
create table ESU_2
as
select * from ESU_1 t
where t.emp_id>20;
When I used below query to get table definition
select dbms_metadata.get_ddl('TABLE', 'ESU_2','SNAPREP') from dual;
I got this o/p
CREATE TABLE ESU_2
( EMP_ID NUMBER(10),
EMP_NAME VARCHAR2(100)
);
But I want the exact table definition that is
create table ESU_2
as
select * from ESU_1 t
where t.emp_id>20;
How can I get this?
When you run
create table ESU_2
as
select * from ESU_1 t
where t.emp_id>20;
internally it will check the definiton of ESU_1 and create a similar table.
create table ESU_2
(
emp_id NUMBER(10),
emp_name VARCHAR2(100)
);
Then it will insert all the matching rows into the table:
insert into ESU_2 select * from ESU_1 where t.emp_id>20;
and perform a commit to pretend that the whole operation is DDL:
commit;
That is why the table definition you get is exactly what the table definition is.
Why is the original DDL not saved? Because it does not make any sense. If later on you change the content of ESU_1 or even the structure of ESU_1 the ESU_2 will not be automatically updated, so the initial query cannot create the same table as it was before.
However it makes total sense to store the DDL for views and materialized views (because materialized view in fact is a combination of a view and a table); it is stored and you can always retrieve it.
I want to create table to save all CONSTRAINTs changes in my oracle database,
so i have created this table(table name , constraint name , date , mode like [insert|update|delete] )
CREATE TABLE CONS
(
C_ID NUMBER NOT NULL
, C_NAME VARCHAR2(50) NOT NULL
, T_NAME VARCHAR2(50) NOT NULL
, EXE_DATE DATE NOT NULL
, MODE VARCHAR2(50) NOT NULL
);
the problem was by insert data,
I was thinking of creating trigger on user_cons_columns after insert or update or delete,
but I found that user_cons_columns is a view and i can't create trigger on it,
so how can I do this work?
or what is the tables that I can create trigger on it to do this???
thanks .......
User_Cons_Columns is metadata view in oracle and you can not use it for monitoring constraint changes.
I think, You can use this metadata view :
SELECT *
FROM ALL_CONSTRAINTS T
It shows which constraint has changed by LAST_CHANGE Column and other data that you can use them.
I don't know if we can create DML TRIGGERs on data dictionary tables in Oracle, but that sounds like a bad idea.
My suggestion to you would be to create a DDL Trigger on the ALTER event
Firstly, as a one time activity, you could store all the available constraints in your CONS table.
Then in your trigger, you could use these conditions to check if a table and column was altered.
if (ora_sysevent = 'ALTER' and
ora_dict_obj_type = 'TABLE')
then alter_column :=
ora_is_alter_column('FOO');
end if;
You could then query the user_cons_columns , ALL_CONSTRAINTS and CONS - whichever you find is relevant to store your data - to find if a new constraint was added or not. If it was indeed added or modified, make an entry into CONS or else update it ( using MERGE statement )
As you said, you can't create that type of a trigger on USER_CONS_COLUMNS:
SQL> create or replace trigger trg_myucc
2 before insert
3 on user_cons_columns
4 begin
5 null;
6 end;
7 /
create or replace trigger trg_myucc
*
ERROR at line 1:
ORA-25001: cannot create this trigger type on this type of view
Why wouldn't we try INSTEAD OF TRIGGER?
SQL> create or replace trigger trg_myucc
2 instead of insert
3 on user_cons_columns
4 for each row
5 begin
6 null;
7 end;
8 /
on user_cons_columns
*
ERROR at line 3:
ORA-01031: insufficient privileges
OK, that won't work either. But, what prevents us on creating a view upon a view, and then create INSTEAD OF trigger on that newly created view?
SQL> create or replace view my_ucc as select * from user_cons_columns;
View created.
SQL> create or replace trigger trg_myucc
2 instead of insert
3 on my_ucc
4 for each row
5 begin
6 null;
7 end;
8 /
Trigger created.
Fine; now you have a way to do what you're supposed to do. More about triggers here.
Is it possible to select from a bulk collection?
Something along these lines:
DECLARE
CURSOR customer_cur IS
SELECT CustomerId,
CustomerName
FROM Customers
WHERE CustomerAreaCode = '576';
TYPE customer_table IS TABLE OF customer_cur%ROWTYPE;
my_customers customer_table;
BEGIN
OPEN customer_cur;
FETCH customer_cur
BULK COLLECT INTO my_customers;
-- This is what I would like to do
SELECT CustomerName
FROM my_customers
WHERE CustomerId IN (1, 2, 3);
END;
I don't seem to be able to select from the my_customers table.
Yes, you can. Declare yourself schema-level types as follows:
create or replace rec_customer_cur
as
object (
customerid integer, -- change to the actual type of customers.customerid
customername varchar2(100) -- change to the actual type of customers.customername
);
/
create or replace type customer_table
as
table of rec_customer_cur;
/
Then, in your PLSQL code, you can declare
CURSOR customer_cur IS
SELECT new rec_customer_cur(CustomerId, CustomerName)
FROM Customers
WHERE CustomerAreaCode = '576';
... and then use ...
SELECT CustomerName
INTO whatever
FROM table(my_customers)
WHERE CustomerId IN (1, 2, 3);
This is because schema-level types can be used in SQL context.
If you want to also display the dataset returned by the select, then just use a REF CURSOR as an OUT parameter.
The SELECT ...FROM TABLE is a SQL statement, which needs a STATIC TABLE NAME, as a database object. It throws an error since the collection name is not actually a database table as an object.
To return the dataset, use SYS_REFCURSOR as OUT parameter.
open cur as select....
I have the following table, two types based on it, and a function that reads from this table:
CREATE TABLE myTable (
ID RAW(16) NULL,
NAME NVARCHAR2(200) NULL,
ENTITYID RAW(16) NOT NULL
);
CREATE TYPE myRowType AS OBJECT (
NAME NVARCHAR2(200),
ENTITYID RAW(16)
);
CREATE TYPE myTableType IS TABLE OF myRowType;
CREATE FUNCTION myFunction(...) RETURN myTableType ...
As you can see, the type myRowType is similar to myTable, but not exactly.
My goal is to insert rows into myTable based on the results of myFunction.
The naive approach would be to just write:
INSERT INTO myTable(ID, NAME, ENTITYID)
SELECT sys_guid(), NAME, ENTITYID
FROM TABLE(myFunction(...));
But since myFunction reads from myTable, this leads to the following error:
ORA-04091: table myTable is mutating, trigger/function may not see it
So I have to split the myFunction call from the insert statement. I tried it like this:
DECLARE
tbl myTableType;
BEGIN
SELECT myRowType(x.NAME, x.ENTITYID)
BULK COLLECT INTO tbl
FROM TABLE(myFunction(...)) x;
INSERT INTO myTable
(ID, NAME, ENTITYID)
SELECT sys_guid(), x.NAME, x.ENTITYID
FROM tbl x;
END;
But here, Oracle doesn't seem to understand the FROM tbl clause. It shows the error
ORA-00942: table or view does not exist
How can I insert the rows in tbl into myTable?
Since you can't use a locally defined nested table as an argument for TABLE function, maybe you would consider using the FORALL bulk insert? I see you are using Oracle 11g, so you will be able to access fields of myRowType. You would then replace your INSERT from your PL/SQL block with this:
FORALL v_i IN tbl.FIRST..tbl.LAST
INSERT INTO myTable VALUES (sys_guid(), tbl(v_i).name, tbl(v_i).entityid);
I recommend this great article by Tim Hall: BULK COLLECT & FORALL