Oracle multiple statments do not run correctly - oracle

I have a file with 3 "create type" statements. I do not understand why, when I run the script it creates only the first type. When I open created type I see all three create statements inside. What am I missing here?
CODE:
create type SplitPathTableType as table of varchar2(450);
create TYPE TSMNodesRecord AS OBJECT (
NodeID number(20),
IsDataNode number(1),
Path nvarchar2 (450),
ParentID number(20),
TimeStep number(20)
);
create type TSMNODESTABLE as table of TSMNODESRECORD;

You need / after each statement...
create type SplitPathTableType as table of varchar2(450);
/
create TYPE TSMNodesRecord AS OBJECT (
NodeID number(20),
IsDataNode number(1),
Path nvarchar2 (450),
vParentID number(20),
TimeStep number(20)
);
/
create type TSMNODESTABLE as table of TSMNODESRECORD;
/

Related

ORA-00909 error when Insert object in object table

I am getting an error ORA-00909 in when inserting an object of a superType in an object table. These are the definitions for the objects:
CREATE OR REPLACE TYPE address AS OBJECT (
street VARCHAR(20),
country VARCHAR(20),
province VARCHAR(20),
city VARCHAR2(20),
zipcode VARCHAR(10)
) FINAL;
CREATE OR REPLACE TYPE company AS OBJECT (
CIF VARCHAR2(9),
code VARCHAR2(10),
name VARCHAR2(20),
signUpDate DATE,
email VARCHAR2(20),
adminAddress address
) NOT FINAL;
CREATE OR REPLACE TYPE inCourseCompany UNDER company (
postalAddress address,
numEmployees NUMBER
) FINAL;
And the object table:
CREATE TABLE companies_objtab OF company (PRIMARY KEY CIF) OBJECT IDENTIFIER IS PRIMARY KEY;
I try to insert an object with the following statement:
INSERT INTO companies_objtab VALUES (
company('J12345678','000001','Test Company',TO_DATE(sysdate, 'dd/mm/yyyy hh24:mi:ss'),'',address('','','','',''))
);
and I get error Error
SQL: ORA-00909: invalid number of arguments
00909. 00000 - "invalid number of arguments"
However, when I insert an object of the subtype inCourseCompany it is inserted correctly:
INSERT INTO companies_objtab VALUES (
inCourseCompany('G11111111','','',TO_DATE(sysdate, 'dd/mm/yyyy hh24:mi:ss'),'',address('','','','',''), address('','','','',''), 100)
);
Any hint about what causes the error?
I am using Oracle SQL Developer 4.0.2.15.21 and Oracle Database Express Edition 11g Release 2.
Thank you in advance.
i tried all your statements right as you posted them (copy-paste). Both on 12c and 11gR2. Everything worked, both inserts. The only thing I noticed is that your create table is a bit incorrect. This is the correct one (but this does not explain the error you get). HTH KR
CREATE TABLE companies_objtab OF company (CIF PRIMARY KEY) OBJECT IDENTIFIER IS PRIMARY KEY;
Maybe try dropping the table and creating it again.

Oracle TYPE declaration throwing error PLS-00201 - identifier must be declared

I created a Object Type successfully using below command:
create or replace
TYPE "SharedAccountRecType" AS object(
account_id NUMBER(11),
share_by_id NUMBER(11),
share_to_id NUMBER(11)
);
Then I tried to create another Type as a table of above successfully created Object Type. Below command is throwing error:
create or replace
TYPE "SharedAccountRecTypeCollection"
as table of SharedAccountRecType
Error: PLS-00201: identifier 'SHAREDACCOUNTRECTYPE' must be declared.
Both above commands were executed using same Oracle user, one after the other. Please help.
If you use double quotes, Oracle will create a type with the exact name you typed, case sensitive.
For example:
SQL> create or replace
2 TYPE "SharedAccountRecType" AS object(
3 account_id NUMBER(11),
4 share_by_id NUMBER(11),
5 share_to_id NUMBER(11)
6 );
7 /
Type created.
SQL> create or replace
2 TYPE SharedAccountRecType AS object(
3 account_id NUMBER(11),
4 share_by_id NUMBER(11),
5 share_to_id NUMBER(11)
6 );
7 /
Type created.
SQL> select type_name
2 from user_types
3 where upper(type_name) = 'SHAREDACCOUNTRECTYPE';
TYPE_NAME
------------------------------
SHAREDACCOUNTRECTYPE
SharedAccountRecType
SQL>
Given this, you may want to remove the double quotes from the creation of SharedAccountRecType:
create or replace TYPE SharedAccountRecType AS object ...
create or replace ... as table of SharedAccountRecType
or add them in the creation of the table type, depending whether you want case sensitive names or not
create or replace TYPE "SharedAccountRecType" AS object ...
create or replace ... as table of "SharedAccountRecType"
It is happened because you use "" in the name of type.
In Oracle names "SharedAccountRecType" and SharedAccountRecType are different.
Don't use name in "", because Oracle threat names in "" as case-sensitive, but name without "" as case-unsensitive uppercase.

Error in creating nested table involving inheritence

I have a somewhat complex structure as follows :
create or replace type user_typ as object(
user_id number(19,0),
username nvarchar2(40 char)
);
I inherit an applicant_typ from this :
create or replace type applicant_typ under user_typ (
resume_text nclob
);
My design involves jobs to which applicants can apply. To this end, I create an application_typ as follows :
create or replace TYPE Application_typ AS OBJECT (
application_id NUMBER,
candidate applicant_typ,
time_of_app DATE
);
CREATE TYPE Application_tab IS TABLE OF Application_typ;
And now I want to create an object type called Job_typ, and a table containing those objects, wherein there will be a nested table for applications :
CREATE OR REPLACE TYPE Job_typ AS OBJECT (
job_ID NUMBER,
company_ID NUMBER,
description NVARCHAR2(1000),
name NVARCHAR2(200),
application Application_tab,
MAP MEMBER FUNCTION job_no RETURN NUMBER,
MEMBER PROCEDURE no_of_applicants
);
All of this works fine. The issue is when I try to create a table of type Job_typ :
CREATE TABLE Job_tab OF Job_typ
NESTED TABLE application STORE AS application_nt;
This doesn't work, giving the error :
SQL Error: ORA-02320: failure in creating storage table for nested table column APPLICATION
ORA-22913: must specify table name for nested table column or attribute
02320. 00000 - "failure in creating storage table for nested table column %s"
*Cause: An error occurred while creating the storage table for the
specified nested table column.
What am I doing wrong?
EDIT : I tried some different things. If I change application_typ as follows :
CREATE OR REPLACE TYPE Application_typ AS OBJECT (
application_id NUMBER,
candidate User_Typ, -- NOTE: This attribute is now of type User_typ instead of the inherited type
time_of_app DATE,
);
CREATE TYPE Application_tab IS TABLE OF Application_typ;
Then everything else works, and I am able to create the Job table. Why do I get the error on using the inherited type?
I tried the following in Oracle 11.2.0.1 and didn't get any error. I made a slight change though:
CREATE OR REPLACE TYPE user_typ AS OBJECT
(
user_id NUMBER (19, 0),
username NVARCHAR2 (40 CHAR)
) NOT FINAL; -- << Notice the NOT FINAL keyword
create or replace type applicant_typ under user_typ (
resume_text nclob
);
CREATE OR REPLACE TYPE Application_typ AS OBJECT
(
application_id NUMBER,
candidate applicant_typ,
time_of_app DATE
);
CREATE TYPE Application_tab IS TABLE OF Application_typ;
CREATE OR REPLACE TYPE Job_typ AS OBJECT
(
job_ID NUMBER,
company_ID NUMBER,
description NVARCHAR2 (1000),
name NVARCHAR2 (200),
application Application_tab,
MAP MEMBER FUNCTION job_no
RETURN NUMBER,
MEMBER PROCEDURE no_of_applicants
);
CREATE TABLE Job_tab OF Job_typ
NESTED TABLE application
STORE AS application_nt;
While trying to create all your types and keywords, I got the error:
[Error] PLS-00590 (10.1): PLS-00590: attempting to create a subtype UNDER a FINAL type
This is because Oracle doesn't allow creation of a subtype on a FINAL type. If you don't define any finalizing clause for the base type, the default is FINAL.
Read more on Oracle Docs.
If you are coding for the real world (read industry), I'd advise against using nested tables as column types. You end up spending your entire life trying to nest and un-nest these. I'd suggest you normalize your schema as much as you can or need and leave nested tables for operations in PL/SQL code blocks.

Oracle error with Select into in memebr function in object type

I'm having trouble with setting a member function inside of a type. I'm new to PL/SQL and having been trying to solve this for days without success.
I am aiming to have a method in singleDeck_type that will count the number of different body model types.
After much playing I have worked out that the only issue seems to be with my select statement, but the only error I receive is "Warning: execution completed with warning".
create or replace type bodyModel_Type as object(
modelID int,
modelName varchar2(45),
floorType varchar2(5),
manufacturer varchar2(45),
bLength number(8,2),
bWidth number(8,2),
noOfAxles int);
/
create table bodyModel_table of bodyModel_Type;
alter table bodyModel_table
Add (constraint bodyModel_PK primary key (modelID));
create or replace type engineModel_type as object(
engineModelID int,
engineDescription varchar2(45),
engineType varchar2(25),
engineCapacity int);
/
create table engineModel_table of engineModel_type;
alter table engineModel_table
Add (constraint engineModel_PK primary key (engineModelID));
create or replace type accessory_type as object(
accessoryID int,
accessoryName varchar2(45),
accessoryDescription varchar2(45),
installDate date);
/
create or replace type table_accessory_type as table of accessory_type;
/
create or replace type bus_type as object(
busID int,
registrationNo varchar2(10),
registrationExpireDate date,
bodyModel REF bodyModel_type,
engineModel Ref EngineModel_type)
not Final;
/
create table bus_table of bus_type;
alter table bus_table
Add (constraint bus_pk primary key (busID));
create or replace type singleDeck_type under bus_type(
noOfDoors int,
seatingCapacity int,
standingCapacity int);
/
create or replace type doubleDeck_type under bus_type(
lowerDeckSeatingCap int,
LowerDeckStandingCap int,
luggageCapacity number(8,2),
upperDeckCapacity int);
/
create table singleDeck_Table of singleDeck_Type;
alter table singleDeck_Table
Add (constraint SD_bus_pk primary key (busID));
create table doubleDeck_Table of doubleDeck_Type;
alter table doubleDeck_Table
Add (constraint DD_bus_pk primary key (busID));
ALTER TYPE singleDeck_type ADD MEMBER procedure count_bodytype
RETURN NUMBER CASCADE;
/
CREATE OR REPLACE TYPE BODY singleDeck_type
AS
MEMBER FUNCTION count_bodytype
RETURN NUMBER
IS
N REAL;
BEGIN
SELECT COUNT(S.busID)
INTO N
FROM singleDeck_Table s
WHERE DEREF(s.bodyRef)=self;
RETURN N;
END count_bodytype;
END;
/
Please, please HELP!!
As OracleUser said, you can add show errors after each PL/SQL section in your script to show any errors generated; or at any time you can query the user_errors view to see the errors against all of your PL/SQL objects.
You're adding a member procedure but specifying a return type in the object specification. If it's returning something then it has to be a function, and you've done that correctly in the body.
ALTER TYPE singleDeck_type ADD MEMBER function count_bodytype
RETURN NUMBER CASCADE;
/
The body does still have an error though:
ORA-00904: "S"."BODYREF": invalid identifier
You have a bodyModel field which is a REF, so I can only think you want to do:
WHERE DEREF(s.bodyModel)=DEREF(self.bodyModel);
or
WHERE DEREF(s.bodyModel).modelID=DEREF(self.bodyModel).modelID;
Although your query seems to be counting the number of single-deck buses with the same body model as the current one, which doesn't match what you said in the question, "count the number of different body model types". That sounds like something you'd need a static method for, not a member method.

Where does oracle store my object instances?

I've created the following two object types :
create or replace type person_typ as object (
person# varchar(10)
) not final;
create or replace type salesperson_typ under person_typ (
salesperson# varchar(10),
sSurname varchar(10),
sForename varchar(10),
dateOfBirth date
);
create table person_tab of person_typ (
person# primary key
);
And I've inserted a row using :
insert into person_tab
values (salesperson_typ('p1','s1', 'Jones', 'John', sysdate));
Which I can retrieve using the following :
select
treat(value(s) as salesperson_typ).person# as person_number,
treat(value(s) as salesperson_typ).sSurname as sSurname
from
person_tab s
;
However, if I look at person_tab I only see the following :
SQL> select * from person_tab;
PERSON#
----------
p1
I'm curious, where does the salesperson specific data get stored? I was almost expecting to find a salesperson table, but I can't find anything obvious.
Your object is stored invisibly in the same table.
You can check columns by querying USER_TAB_COLS:
SELECT *
FROM user_tab_cols
WHERE table_name = 'PERSON_TAB';
Then you can then use the column names* you just discovered in a query (except SYS_NC_ROWINFO$, that throws an error for me).
SELECT SYS_NC_OID$
,SYS_NC_TYPEID$
--,SYS_NC_ROWINFO$
,PERSON#
,SYS_NC00005$
,SYS_NC00006$
,SYS_NC00007$
,SYS_NC00008$
FROM PERSON_TAB;
Note*
You should not use these column names in any application because they are internal and subject to change in future patches/releases.

Resources