Oracle Object-Relational - PLS-00538 error and PLS-00539 error - sql

Type definition:
CREATE TYPE INTERVENTO_TY AS OBJECT(
Testo VARCHAR2(20),
Timestmp DATE
) NOT FINAL;
CREATE TYPE COMMENTO_TY UNDER INTERVENTO_TY(
Ordine VARCHAR2(20)
);
CREATE TYPE POST_TY UNDER INTERVENTO_TY(
Titolo VARCHAR2(20),
MEMBER PROCEDURE AddCom(Ordine VARCHAR2, Testo VARCHAR2, Timestmp Date)
);
CREATE TYPE AUTORE_TY AS OBJECT(
Nome VARCHAR2(20),
Cognome VARCHAR2(20),
IdAutore INT
);
CREATE TYPE MEDIA_TY AS OBJECT(
Tipo VARCHAR2(20),
Nome VARCHAR2(20),
IdMedia INT,
Titolo VARCHAR2(20)
);
CREATE TYPE SCRIVE_INTERVENTO AS TABLE OF REF INTERVENTO_TY;
CREATE TYPE DI_AUTORE AS TABLE OF REF AUTORE_TY;
ALTER TYPE INTERVENTO_TY ADD ATTRIBUTE DI DI_AUTORE CASCADE;
ALTER TYPE AUTORE_TY ADD ATTRIBUTE SCRIVE SCRIVE_INTERVENTO CASCADE;
CREATE TYPE IN_INTERVENTO AS TABLE OF REF INTERVENTO_TY;
CREATE TYPE CONTIENE_MEDIA AS TABLE OF REF MEDIA_TY;
ALTER TYPE INTERVENTO_TY ADD ATTRIBUTE CONTIENE CONTIENE_MEDIA CASCADE;
ALTER TYPE MEDIA_TY ADD ATTRIBUTE IN_INT IN_INTERVENTO CASCADE;
CREATE TYPE COMMENTATO_POST AS TABLE OF REF COMMENTO_TY;
ALTER TYPE COMMENTO_TY ADD ATTRIBUTE A REF POST_TY CASCADE;
ALTER TYPE POST_TY ADD ATTRIBUTE COMMENTATO COMMENTATO_POST CASCADE;
CREATE TABLE AUTORE_TAB OF AUTORE_TY
NESTED TABLE SCRIVE STORE AS SCRIVE_NESTED;
CREATE TABLE INTERVENTO_TAB OF INTERVENTO_TY
NESTED TABLE DI STORE AS DI_NESTED
NESTED TABLE CONTIENE STORE AS CONTIENE_NESTED;
CREATE TABLE MEDIA_TAB OF MEDIA_TY
NESTED TABLE IN_INT STORE AS IN_INT_NESTED;
I'm trying to write an object procedure that creates a new instance of COMMENTO_TY, adds it to the COMMENTO_TAB table and the COMMENTATO nested table of POST_TY, but it gives me the following error:
CREATE OR REPLACE TYPE BODY Post_ty AS
MEMBER PROCEDURE addComm(commento VARCHAR2, ordine NUMBER, IdAut INTEGER) AS
NuovoCommento REF Commento_ty;
BEGIN
INSERT INTO Intervento_tab I
VALUES(Commento_ty(commento, SYSTIMESTAMP,
DI_AUTORE(
(SELECT REF(A) FROM AUTORE_TAB A WHERE IdAutore = IdAut)
),
CONTIENE_MEDIA(),
ordine,
(SELECT TREAT(REF(I) AS REF POST_TY)
FROM INTERVENTO_TAB I
WHERE VALUE(I) IS OF (POST_TY) AND
I.TIMESTMP = SELF.TIMESTMP)))
RETURNING REF(I) INTO NuovoCommento;
INSERT INTO TABLE(SELECT TREAT(VALUE(I) AS POST_TY).COMMENTATO
FROM INTERVENTO_TAB I
WHERE VALUE(I) IS OF (POST_TY)
AND I.TIMESTMP = SELF.TIMESTMP)
VALUES(NuovoCommento);
END addComm;
END;
/
PLS-00538: subprogram or cursor 'ADDCOM' is declared in an object type specification and must be defined in the object type body at line 3
PLS-00539: subprogram 'ADDCOMM' is declared in an object type body and must be defined in the object type specification at line 2

You have two errors:
PLS-00538: subprogram or cursor 'ADDCOM' is declared in an object type specification and must be defined in the object type body
and
PLS-00539: subprogram 'ADDCOMM' is declared in an object type body and must be defined in the object type specification
Notice how one is spelt ADDCOM and the other is ADDCOMM; you need to make sure the member procedure has the same name in the specification and the body.

Related

ORA-02330: datatype specification not allowed

I tried creating a an object table after successfully creating an object type but i got the error
'datatype specification not allowed'.
Please what am i doing wrong. I am lost at this point.
CREATE OR REPLACE Type Route_t AS Object(
Route_ID CHAR(3),
Route_descr VARCHAR(30),
city VARCHAR(10),
Stop_no NUMBER(3),
Stop_meal VARCHAR(10),
Route_ticket ticket_nt_type,
Route_schedule schedule_nt_type
);
Create Table Route_Tab of Route_t
(primary key (Route_ID),
Nested Table Route_ticket Store As Route_ticket_NTab,
Nested Table Route_Schedule Store As Route_Schedule _NTab;
);
The NESTED TABLE storage clauses must come after the parenthesis. Below is a fully functional example.
--Drop table and types to reset environment.
/*
drop table route_tab;
drop type route_t;
drop type ticket_nt_type;
drop type schedule_nt_type;
*/
--Create nested types.
create or replace type ticket_nt_type as table of varchar2(100);
create or replace type schedule_nt_type is table of varchar2(100);
--Create object.
CREATE OR REPLACE Type Route_t AS Object(
Route_ID CHAR(3),
Route_descr VARCHAR(30),
city VARCHAR(10),
Stop_no NUMBER(3),
Stop_meal VARCHAR(10),
Route_ticket ticket_nt_type,
Route_schedule schedule_nt_type
);
--ORIGINAL version that throws "ORA-02330: datatype specification not allowed".
Create Table Route_Tab of Route_t
(primary key (Route_ID),
Nested Table Route_ticket Store As Route_ticket_NTab,
Nested Table Route_Schedule Store As Route_Schedule _NTab;
);
--NEW version that runs on my system.
Create Table Route_Tab of Route_t
(primary key (Route_ID))
Nested Table Route_ticket Store As Route_ticket_NTab,
Nested Table Route_Schedule Store As Route_Schedule_NTab;

How to update reference oracle

Im making two object tables, with reference M:M, insert is working correctly, but when im trying to update reference, i get the error.
Error:
SQL Error: ORA-00904: "A"."ATS_PN": "%s: invalid identifier"
CREATE OR REPLACE TYPE STUDENTI AS OBJECT(
S_NUM INT,
S_UZV VARCHAR2(30)
);
CREATE OR REPLACE TYPE PASNIEDZEJI AS OBJECT(
P_NUM INT,
P_UZV VARCHAR2(30)
);
CREATE OR REPLACE TYPE ATS_ST AS OBJECT(
ID INT,
ST_ID REF STUDENTI
);
CREATE OR REPLACE TYPE ATS_PN AS OBJECT(
ID INT,
PN_ID REF PASNIEDZEJI
);
CREATE OR REPLACE TYPE ATS_P AS TABLE OF ATS_PN;
CREATE OR REPLACE TYPE ATS_S AS TABLE OF ATS_ST;
CREATE OR REPLACE TYPE O_STUDENTI AS OBJECT(
STUD STUDENTI,
PASN ATS_P
);
CREATE OR REPLACE TYPE O_PASNIEDZEJI AS OBJECT(
PASN PASNIEDZEJI,
STUD ATS_S
);
CREATE TABLE T_STUDENTI OF O_STUDENTI
NESTED TABLE PASN STORE AS P_TBL;
CREATE TABLE T_PASNIEDZEJI OF O_PASNIEDZEJI
NESTED TABLE STUD STORE AS S_TBL;
INSERT INTO T_STUDENTI VALUES (STUDENTI(191, 'BRONES'), NULL);
INSERT INTO T_PASNIEDZEJI VALUES (PASNIEDZEJI(53, 'JHONES'), NULL);
INSERT INTO T_PASNIEDZEJI VALUES (PASNIEDZEJI(54, 'HIKKAEV'), NULL);
UPDATE T_STUDENTI A SET A.PASN = (1, 'TEST') WHERE A.STUD.S_NUM = 191; --misstake some where ther :/
I'm not following what you are doing functionally, but I've removed the REF from your objects because it's hard to deal with in SQL.
You'll notice that t_studenti.pasn is ats_p which is a table of ats_pn which contains an id and a pasniedzeji: you must construct that entire hierarchy.
This code will let you update the table:
CREATE OR REPLACE TYPE STUDENTI AS OBJECT(
S_NUM INT,
S_UZV VARCHAR2(30)
);
CREATE OR REPLACE TYPE PASNIEDZEJI AS OBJECT(
P_NUM INT,
P_UZV VARCHAR2(30)
);
CREATE OR REPLACE TYPE ATS_ST AS OBJECT(
ID INT,
ST_ID STUDENTI
);
CREATE OR REPLACE TYPE ATS_PN AS OBJECT(
ID INT,
PN_ID PASNIEDZEJI
);
CREATE OR REPLACE TYPE ATS_P AS TABLE OF ATS_PN;
CREATE OR REPLACE TYPE ATS_S AS TABLE OF ATS_ST;
CREATE OR REPLACE TYPE O_STUDENTI AS OBJECT(
STUD STUDENTI,
PASN ATS_P
);
CREATE OR REPLACE TYPE O_PASNIEDZEJI AS OBJECT(
PASN PASNIEDZEJI,
STUD ATS_S
);
CREATE TABLE T_STUDENTI OF O_STUDENTI
NESTED TABLE PASN STORE AS P_TBL;
CREATE TABLE T_PASNIEDZEJI OF O_PASNIEDZEJI
NESTED TABLE STUD STORE AS S_TBL;
INSERT INTO T_STUDENTI VALUES (STUDENTI(191, 'BRONES'), NULL);
INSERT INTO T_PASNIEDZEJI VALUES (PASNIEDZEJI(53, 'JHONES'), NULL);
INSERT INTO T_PASNIEDZEJI VALUES (PASNIEDZEJI(54, 'HIKKAEV'), NULL);
UPDATE t_studenti a
SET a.pasn = ats_p(ats_pn(1, -- ??
pasniedzeji(1,
'TEST')))
WHERE a.stud.s_num = 191;

Oracle OR - how to guarantee that a nested table has references to all the subtypes of a supertype

I'm designing an object-relational model database. I have a supertype named "topico_t" and 4 subtypes of this supertype named "anotacao_t", "tarefa_t", "memo_t" and "contacto_t". See here the DDL snippet:
CREATE OR REPLACE TYPE categoria_t AS OBJECT(
nome VARCHAR2(25),
pai REF categoria_t
);
CREATE OR REPLACE TYPE categoria_tab_t AS TABLE OF REF categoria_t;
CREATE OR REPLACE TYPE topico_t AS OBJECT(
titulo VARCHAR2(100),
ultimaAlteracao DATE,
categorias categoria_tab_t
--referencias topico_tab_t
) NOT FINAL;
CREATE OR REPLACE TYPE topico_tab_t AS TABLE OF REF topico_t;
ALTER TYPE topico_t ADD ATTRIBUTE referencias topico_tab_t CASCADE;
CREATE OR REPLACE TYPE periodo_t AS OBJECT(
inicio DATE,
fim DATE
);
CREATE OR REPLACE TYPE repeticao_t AS OBJECT(
frequencia VARCHAR2(10),
duracao periodo_t
);
CREATE OR REPLACE TYPE anotacao_t UNDER topico_t(
periodo periodo_t,
repeticao repeticao_t
);
CREATE OR REPLACE TYPE telefone_t AS OBJECT(
numero VARCHAR2(25)
);
CREATE OR REPLACE TYPE telefone_tab_t AS TABLE OF telefone_t;
CREATE OR REPLACE TYPE morada_t AS OBJECT(
rua VARCHAR2(100),
localidade VARCHAR2(50),
codigoPostal VARCHAR2(10)
);
CREATE OR REPLACE TYPE morada_tab_t AS TABLE OF morada_t;
CREATE OR REPLACE TYPE contacto_t UNDER topico_t(
telefones telefone_tab_t,
moradas morada_tab_t,
email VARCHAR2(100),
url VARCHAR2(150)
);
CREATE OR REPLACE TYPE tarefa_t UNDER topico_t(
dataFim DATE,
completo NUMBER(1,0),
conteudo VARCHAR2(255)
);
CREATE OR REPLACE TYPE memo_t UNDER topico_t(
conteudo VARCHAR2(255)
);
CREATE TABLE categorias OF categoria_t;
CREATE TABLE topicos OF topico_t
NESTED TABLE categorias STORE AS categorias_nested
NESTED TABLE referencias STORE AS referencias_nested;
So, then I populate this table "topicos" with this:
INSERT INTO topicos VALUES (tarefa_t(
'Dissertacao',
TO_DATE('2018/02/13', 'YYYY/MM/DD'),
categoria_tab_t((select ref(c) from categorias c where nome='FEUP')),
topico_tab_t((select ref(t) from topicos t where titulo='Diogo Pereira'), -- which is an object of the subtype "contacto_t"
(select ref(t) from topicos t where titulo='Comecar a dissertacao'), -- which is an object of the subtype "memo_t"
(select ref(t) from topicos t where titulo='Apresentar Dissertacao'), -- which is an object of the subtype "tarefa_t"
(select ref(t) from topicos t where titulo='Reuniao com Orientador da Dissertacao')), -- which is an object of the subtype "anotacao_t"
TO_DATE('2018/08/13', 'YYYY/MM/DD'),
0,
'Dissertacao all over again'));
So, I have to build a query or even a PL/SQL block that returns me all the rows of the table "topicos" that contains at least, in the nested table "referencias", one instance object for each of this 4 subtypes mentioned above. Ideally this query would return me that row (mentioned in the above INSERT).
Best regards and hope you're having a good Worker's Day! ;)
You have a mistake in your DDL code. You are creating a base type topico_t and then some subtypes under that type, but you're creating a single table of base type objects only. The problem with this is that you're expecting that table to hold objects of any subtype, which will not be the case due to what is known as object slicing. The table you've created has only the columns which correspond to the fields you've defined for the base type.
Therefore, you need to create tables for all of the subtypes which you will eventually instantiate. You should only create a table for the base type if it makes sense for that type to be instantiated, that is, if an object of the base type which doesn't belong to any of the subtypes could exist.
After doing this, the query you're trying to write should use the function TREAT. Note that, according to the documentation, this function returns NULL when the expression is not of the type you're trying to cast to, so the query you need to write should be as simple as testing if there is at least one row with a non-null result field when selecting from the nested table and attempting to cast to each of the subtypes you've created. Check the example on the documentation to see how to use this function.

Invalid Identifier error for scope

I have created customer_ty object which includes a nested table called "deposits".
CREATE TYPE deposit_ty as object(
depNo number,
depCategory ref depcategory_ty,
amount number,
period number
)
/
This 'depCategory' reference depcategory_ty in another table called 'depcategory_tbl'.
CREATE TYPE deposit_ntty as table of depcategory_ty
/
CREATE TYPE address_ty as varray(3) of varchar2(20)
/
CREATE TYPE customer_ty as object(
custId varchar2(4),
custName varchar2(10),
address address_ty,
dob date,
deposits deposit_ntty
)
/
CREATE TABLE customer_tbl of customer_ty(
custId primary key)
nested table deposits store as cli_deposit_tbl
/
alter table cli_deposit_tbl
add scope for (depCategory) is depcategory_tbl
/
The problem appears when I try to add a scope for table. it says;
add scope for (depCategory) is depcategory_tbl
*
ERROR at line 2:
ORA-0094:"DEPCATEGORY":Inavalid Identifier
All the identifiers are correct. What is wrong with this?
It looks like you've defined deposit_ntty incorrectly, and meant it to be:
CREATE TYPE deposit_ntty as table of deposit_ty
/
With that change, your alter now gets:
alter table cli_deposit_tbl
add scope for (depCategory) is depcategory_tbl
/
ORA-22892: scoped table "DEPCATEGORY_TBL" does not exist in schema "PUBLIC"
You may already have that but it isn't shown; with it there the alter works:
CREATE TABLE depcategory_tbl of depcategory_ty;
Table DEPCATEGORY_TBL created.
alter table cli_deposit_tbl
add scope for (depCategory) is depcategory_tbl
/
Table CLI_DEPOSIT_TBL altered.

Type Inheritance in oracle 10g

I am trying to implement type inheritance in oracle 10g
Hereis my code:
create type stud_detail as object
(stud_id number(5),
stud_name varchar(8));
/
create type stud_result under stud_detail as
(status(P/F)? char(1));
/
I get this error:
ERROR at line 1: PLS-00103: Encountered the symbol "AS" when expecting
one of the following:
. ( not external JAVA_ BOUND_
1. create type stud_result under stud_detail as object
2. (status(P/F)? char(1));
There are two things you need to do:
Declare your stud_detail type as NOT FINAL. You can only inherit from a type that is NOT FINAL, and types are FINAL by default.
Lose the AS keyword in your create type stud_result under stud_detail... line.
Demonstration:
SQL> create type stud_detail as object (stud_id number(5), stud_name varchar2(8)) not final;
2 /
Type created.
SQL> create type stud_result under stud_detail ("status(P/F)?" char(1));
2 /
Type created.
SQL> desc stud_result;
stud_result extends LUKE.STUD_DETAIL
Name Null? Type
----------------------------------------- -------- ----------------------------
STUD_ID NUMBER(5)
STUD_NAME VARCHAR2(8)
status(P/F)? CHAR(1)
Note that I've changed varchar to varchar2, and put quotes around the name of the column status(P/F)?.