How to update reference oracle - sql

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;

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;

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

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.

Is there way to create trigger for get ref value when insert data?

I create table using types in oracle. I have data file in csv. I need to get ref value for patient table when inserting data.
CREATE TYPE Location_t as OBJECT(
L_id varchar(6),
lon NUMBER(9,6),
lat NUMBER(9,6),
country varchar(26),
Province varchar(26)
)
/
create type patient_t as object(
p_id varchar(10),
ReportedDate date,
Deadts int,
recover int,
confirmed int,
loc ref location_t
)
create table Location of location_t
(L_id PRIMARY KEY)
/
CREATE TABLE PATIENT of patient_t
(p_id PRIMARY KEY,
loc references Location)
Location table data is already inserted and i need to get ref code for patient.
location table
patient table

How to overcome a persistent oracle 'invalid identifier' error on a basic insert?

I am trying to create a basic table using subtypes and insert some data into this in Oracle Express 11g.
My table is successfully created but i am having issues with inserting data.
The result of my insert statement always throws back an error 'SQL Error: ORA-00904: "BRANCH_PHONE": invalid identifier'.
The column which shows up in the error message is always the column which is at the end of the insert statement, despite the column existing in the table. I have tried the following code:
create type addressType as object(
street varchar2(20),
city varchar2(20),
postCode varchar2(8))
not final
/
create type branchType as object(
branchID int,
branch_address addressType,
branch_phone int(11))
not final
/
create table Branch of branchType(
constraint branch_pk primary key(branchID));
/
insert into Branch values (
branchID('2364'),
addressType('12 Rooster','Atlantis','A13 4UG'),
branch_phone('01316521311'));
I would really appreciate any ideas.
I made some changes, including changing the branch_phone to varchar2. A Phone number, while is "numbers" is not a data type of number. it is a string of characters. Also you were passing branchID as a string, but you are declaring it as a number, so changed that also. BranchID and branch_phone are primitive data types, so no constructor needed.
create type addressType as object(
street varchar2(20),
city varchar2(20),
postCode varchar2(8))
not final
/
create type branchType as object(
branchID int,
branch_address addressType,
branch_phone varchar2(11))
not final
/
create table Branch of branchType(
constraint branch_pk primary key(branchID));
/
insert into Branch values (
branchtype(2364,
addressType('12 Rooster','Atlantis','A13 4UG'),
'01316521311') )

ORA-00936: missing expression error when inserting values

I spend lot of time searching where i made the mistake but i was unable to find it when its going to insert the last record there is a error message showing "ORA-00936: missing expression" How to solve this please help me
create type pearson_types as object(
name varchar2(50),
sysID char(6)
)NOT FINAL;
create type doctor_types under pearson_types(
regNo char(10),
specialization varchar2(25)
)
create table doctor of doctor_types(
regNo primary key
)
create type hospVisits_types as object(
hosChg float,
vDate varchar2(20),
refDoc REF doctor_types,
docChg float
)
create type hospvisits_tbl_types as table of hospVisits_types
create type phone_arr as VARRAY(3) of char(10)
create type patient_types under pearson_types
(
id char(10),
dob varchar(20),
phone phone_arr,
hospVisits hospvisits_tbl_types
)
create table patients of patient_types(
id primary key
)nested table hospVisits store as Hospital_tables
alter table Hospital_tables add scope for (refDoc) is doctor
insert into doctor values ('Dr.k.perera','D001','1223441234','Gynecologist');
insert into doctor values ('Dr.p.weerasingha','D002','1234421131','Dermatalogist');
insert into doctor values ('Prof .S. Fernando','D003','2342111322','Pediatrician');
insert into doctor values ('Dr.k.Sathgunanathan','D004','2344114344','Pediatrician');
insert into patients values('Sampath Weerasingha','P001','732821122V','23-JAN-73',phone_arr('0332124222'),hospvisits_tbl_types(hospVisits_types(50.00,'24-MAY-06',select ref (a) from doctor a where a.regNo='1223441234',500.00)))
Add parentheses to SELECT statements inside a SQL statement:
insert into patients values(
'Sampath Weerasingha','P001','732821122V','23-JAN-73',phone_arr('0332124222'),
hospvisits_tbl_types(hospVisits_types(50.00,'24-MAY-06',
( -- ADD ME
select ref (a) from doctor a where a.regNo='1223441234'
) -- ADD ME
,500.00))
);