Circular Dependency -Table Insertion Error - sql

I want to insert data into table but got below error..Can any one help me..
CREATE OR REPLACE TYPE TEST_TYP FORCE IS OBJECT
("id" VARCHAR(5000 NULL)
NOT FINAL;
CREATE OR REPLACE TYPE TEST_TAB is table of REF TEST_TYP;
CREATE OR REPLACE TYPE TEST1_TYP FORCE IS OBJECT
("id" VARCHAR2(500) NULL,
"extension" "TEST_TAB" NULL )
NOT FINAL;
CREATE TABLE "TEST_OBJ_TABLE" OF "TEST1_TYP"
NESTED TABLE "extension" STORE AS "Allin"
When I try to insert using this statementL
insert into "TEST_OBJ_TABLE" ("id","extension")
VALUES(
'0FE71A85',
"TEST_TAB"("TEST_TYP"( '0FE71A8'))
);
It throws this error
Error at Command Line : 59 Column : 12
Error report -
SQL Error: ORA-00932: inconsistent datatypes: expected REF SUB_HWOW.TEST_TYP got SUB_HWOW.TEST_TYP
00932. 00000 - "inconsistent datatypes: expected %s got %s"
*Cause:
*Action:

You have to modify object TEST_TYP -> varchar2 has to be max 4000.
You have to store this object in db.
Create object table for TEST_TYP
create table t_for_test_type of TEST_TYP;
To obtain object reference, the object have to be save in db table. In your case:
declare
v_ref_to_test_type1 ref TEST_TYP;
begin
insert into t_for_test_type t values('abcd1') return ref(t) into v_ref_to_test_type1;
end:
Join all parts together.
declare
v_ref_to_test_type1 ref TEST_TYP;
v_ref_to_test_type2 ref TEST_TYP;
begin
insert into t_for_test_type t values('abcd1') return ref(t) into v_ref_to_test_type1;
insert into t_for_test_type t values('abcd2') return ref(t) into v_ref_to_test_type2;
insert into TEST_OBJ_TABLE values (TEST1_TYP('abcd',new TEST_TAB(v_ref_to_test_type1,v_ref_to_test_type2)));
end;
Query table.
select t."id",x.column_value from TEST_OBJ_TABLE t, table(t."extension") x it returns id + ref to other object
Viewing the referenced object.
select t."id",deref(x.column_value) from TEST_OBJ_TABLE t, table(t."extension") x
Note1. You should avoid declaring attribute using double quote. Db becomes case sensitive and it's not normal situation :)
Note2. I don't know why code formatting is not working today
Question. Why are you trying to use such complicated construction?

Changed the original answer. The quotes are doing you no favors - nor was calling your column names id. Removed the REF reference.
CREATE OR REPLACE TYPE TEST_TYP FORCE IS OBJECT
(id1 VARCHAR(4000) NULL)
NOT FINAL;
CREATE OR REPLACE TYPE TEST_TAB is table of TEST_TYP;
CREATE OR REPLACE TYPE TEST1_TYP FORCE IS OBJECT
(id2 VARCHAR2(500) NULL,
extension TEST_TAB NULL )
NOT FINAL;
CREATE TABLE TEST_OBJ_TABLE OF TEST1_TYP
NESTED TABLE extension STORE AS Allin ;
insert into TEST_OBJ_TABLE ( ID2, EXTENSION)
VALUES(
'0FE71A85',
TEST_TAB(TEST_TYP( '0FE71A8'))
);
SELECT * FROM TEST_OBJ_TABLE ;

Related

Oracle SQL3 3 nesting tables insert problem

I'm making an SQL3 script where I create types and then create tables and nested tables.
The prolem I'm getting happens when I want to insert a row where it says :
ORA-00932: inconsistent datatypes: expected UDT got CHAR
this is an online reproduction of the problem where the types and tables are already created, you can try to insert:
https://www.tutorialspoint.com/oracle_terminal_online.php?fbclid=IwAR0GgaLe2_GvGsEb80eB-D0uKDSDJDr1WNBPiK3mHQqpJQrtfacQ1cf03NA
the following is the types creation script
CREATE TYPE T_Personne ;
/
CREATE TYPE T_SET_Tag AS TABLE OF Varchar2(30);
/
CREATE TYPE T_Message AS OBJECT (
Texte Varchar2(500),
DateEcrit Date,
Tags T_SET_Tag
);
/
CREATE TYPE T_SET_Message AS TABLE OF T_Message;
/
CREATE TYPE T_Contact AS OBJECT(
Per REF T_Personne,
Depuis Date
);
/
CREATE TYPE T_SET_Contact AS TABLE OF T_Contact;
/
CREATE OR REPLACE TYPE T_Personne AS OBJECT (
Prenom Varchar2(30),
Suit T_SET_Contact,
Ecrit T_SET_Message
);
/
this one is the table creation script
CREATE TABLE TAB_Personne OF T_Personne
NESTED TABLE Suit STORE AS TAB_suit,
NESTED TABLE Ecrit STORE AS TAB_ecrit(
NESTED TABLE Tags STORE AS TAB_Tags
);
and finally the script I'm using to insert my new rows
INSERT INTO TAB_Personne VALUES(
'Baam',
T_SET_Contact(),
T_SET_Message()
);
INSERT INTO TAB_Personne VALUES(
'Rachel',
T_SET_Contact(
(SELECT REF(P)
FROM TAB_Personne P
WHERE P.Prenom='Baam'),
to_date('01/01/2018', 'dd/mm/yyyy')
),
T_SET_Message(
'Paris candidat aux jeux Olympiques 2022',
to_date('01/06/2019', 'dd/mm/yyyy'),
T_SET_Tag('JM2022')
)
);
the error message I'm getting is
1 row created.
'Paris candidat aux jeux Olympiques 2022', *
ERROR at line 10:
ORA-00932: inconsistent datatypes: expected UDT got CHAR
I'd be glad if anyone can guide me through this, thanks.
You are specifying a T_SET_Message collection but you then need T_Message objects within that; you're supplying the attributes of that object type, not an actual object of the type. The first element in the (non-)collection is a string, hence the error you get - you've supplied a string ('Paris...') when it's expecting to see a UDT (T_Message('Paris...', ...)).
You are also doing the same thing with the T_SET_Contact collection.
You need to wrap your current attributes in object constructors; so this works:
INSERT INTO TAB_Personne VALUES(
'Rachel',
T_SET_Contact(
T_Contact(
(SELECT REF(P)
FROM TAB_Personne P
WHERE P.Prenom='Baam'),
to_date('01/01/2018', 'dd/mm/yyyy')
)
),
T_SET_Message(
T_Message(
'Paris candidat aux jeux Olympiques 2022',
to_date('01/06/2019', 'dd/mm/yyyy'),
T_SET_Tag('JM2022')
)
)
);
db<>fiddle

PLS-00103 encoutered the symbol

I already tried to look for this type of error. I do have similar errors on others but different problems. Please help me with this, what is the good approach to this sample oracle query ?
create type Exams (
year char(4),
city varchar(20)
)
create table exams of type Exams
create OR REPLACE type Skills AS OBJECT(
type varchar(20),
ExamSet Exams multiset
);
/
create table skills of Skills;
Here is the error in script output :
LINE/COL ERROR
--------- -------------------------------------------------------------
1/12 PLS-00103: Encountered the symbol "(" when expecting one of the following: ; is authid as compress force compiled wrapped under
Errors: check compiler log
Try :
CREATE OR REPLACE TYPE ExamsType AS OBJECT (
year char(4),
city varchar(20)
);
/
create table Exams OF ExamsType;
Fixes :
create the type AS OBJECT ; this will give you an Abstract Data Type (ADT), that can actually hold attributes - see the oracle docs
you need a semi-colon and a slash at the end of the create type statement
the table cannot have the same name as the type
syntax for CREATE TABLE was wrong
To insert in this table, you would typically use the following statement :
insert into Exams values( ExamsType('2018', 'my adress') );
This will work:
CREATE TABLE new_table
AS (SELECT *
FROM old_table WHERE 1=2);
For your case it goes like this:
CREATE TABLE exams
AS (SELECT *
FROM Exams WHERE 1=2);
for 1=2 it does not select any of the rows but with as it copies the table definition.

Accessing object reference of a row in a trigger after insert

I have a problem with SQL Object (ORACLE). :(
I have an abstract type Forum, generalized in ForumCategory, and a type Category :
CREATE OR REPLACE TYPE Forum_t AS OBJECT
(
moderators Users_table_ref,
topics Topics_table,
MEMBER FUNCTION getName RETURN VARCHAR2
)
NOT FINAL NOT INSTANTIABLE;
/
CREATE TABLE Forum OF Forum_t
NESTED TABLE moderators STORE AS Liste_moderators,
NESTED TABLE topics STORE AS Liste_topics
(NESTED TABLE posts STORE AS Liste_posts);
CREATE OR REPLACE TYPE ForumCategory_t UNDER Forum_t
(
category REF Category_t,
OVERRIDING MEMBER FUNCTION getName RETURN VARCHAR2
);
/
CREATE OR REPLACE TYPE BODY ForumCategory_t AS
OVERRIDING MEMBER FUNCTION getName RETURN VARCHAR2 IS
c Category_t;
BEGIN
SELECT DEREF(SELF.category) INTO c FROM DUAL;
RETURN c.name;
END getName;
END;
/
CREATE OR REPLACE TYPE Category_t AS OBJECT
(
name VARCHAR2(60)
);
/
CREATE TABLE Category OF Category_t
(
CONSTRAINT PK_CAT_NAME PRIMARY KEY(name)
);
I want to make a trigger after insert on table Category which insert a ForumCategory_t on table Forum.
I have this code :
CREATE OR REPLACE TRIGGER Category_insert
AFTER INSERT ON Category
FOR EACH ROW
DECLARE
ref_cat REF Category_t;
BEGIN
SELECT REF(c) INTO ref_cat
FROM Category c
WHERE c.name = :NEW.name;
INSERT INTO Forum VALUES(ForumCategory_t(Users_table_ref(),Topics_table(),ref_cat));
END;
/
The beginning don't work, I cannot select REF of the inserted Category (it's work outside the trigger).
Please, I need help ^^
PS : Sorry for my english

How to use in statement with nested table

Hey there I have a function, and part of the function is to make sure that the selected value is within the passed in table of varchar2s. To start I declare a varchar2 table type like so.
create or replace type Varchar2Table is table of varchar2(200)
Then I have the function which accepts the nested table parameter and has a select statement on them.
function SelectPeople(inputNames Varchar2Table) return People
begin
--stuff
select * from person_table where name in inputNames; --line of interest
--more stuff
end;
This doesn't seem to work though, I get the following error:
ORA-00932: inconsistent datatypes: expected NUMBER got
ENGSPL5.VARCHAR2TABLE
Any suggestions?
The TABLE operator allows nested tables to be used in SQL statements. The function was also missing an IS and an INTO.
create or replace type Varchar2Table is table of varchar2(200);
create table person_table(id number, name varchar2(100));
create or replace function SelectPeople(inputNames Varchar2Table) return number
is --Missing "IS".
type numberTable is table of number; --Need a collection to store results.
numbers numberTable;
begin
select id
bulk collect into numbers --Missing "INTO".
from person_table
where name in (select column_value from table(inputNames)); --Missing "TABLE".
--Alternatively a multiset condition can be used.
--where name member of inputNames;
--Dummy return value to make the function compile.
return 1;
end;
/

Inconsistent datatypes : CAST REF?

I've this schema:
CREATE OR REPLACE TYPE tree_t
/
CREATE OR REPLACE TYPE element_t AS OBJECT (
libelle varchar2(100),
parent REF tree_t,
) NOT FINAL NOT INSTANTIABLE
/
CREATE OR REPLACE TYPE tree_t UNDER element_t()
/
CREATE OR REPLACE TYPE leaf_t UNDER element_t()
/
And I'm trying to insert in this table:
CREATE TABLE elements OF element_t;
For an element_t without parent, no problem :
INSERT INTO elements VALUES (tree_t('first', NULL));
But with a parent :
INSERT INTO elements
VALUES (tree_t('second',
(SELECT REF(e)
FROM elements e
WHERE e.libelle = 'first')));
I've this result:
ORA-00932: inconsistent datatypes: expected REF CINETREE.TREE_T
got REF CINETREE.ELEMENT_T
Can I keep this schema, and how to make insertions?
(with CAST?)
TREAT should do the trick:
INSERT INTO elements
VALUES (tree_t('second',
(SELECT TREAT(REF(e) AS REF tree_t)
FROM elements e
WHERE e.libelle = 'first')));