error- declaration is incomplete or malformed - sql

I have the error- the declaration of ... is incomplete or malformed coming from the last type- "Subscriber_T" but I don't see a problem with the syntax. These are the scripts:
CREATE OR REPLACE TYPE Surnames_T AS OBJECT (
Surname varchar (20)
);
/
CREATE OR REPLACE TYPE listSurnames_T
AS Varray(4) of Surnames_T;
/
CREATE OR REPLACE TYPE listTels as object(
Tel number (12)
);
/
CREATE OR REPLACE TYPE listTels_T as Varray(5) of listTels;
/
CREATE OR REPLACE TYPE ADDRESS_T AS OBJECT (
NUM number (6),
STREET varchar (20),
TOWN varchar (20)
);
/
CREATE or replace type TAddress
as table of Address_T;
/
create or replace type Subscriber_T as object(
num_s number(6),
sName varchar(30),
surname listSurnames_T,
Adds TAddress,
DateOfBirth date,
member function cal_Age return number,
phoneNo listTels_T
);
/
show error
I need a fix please!

You have to put member functions after all other fields; you can't sneak a field in at the end:
create or replace type Subscriber_T as object(
num_s number(6),
sName varchar(30),
surname listSurnames_T,
Adds TAddress,
DateOfBirth date,
phoneNo listTels_T,
member function cal_Age return number
);
/
TYPE SUBSCRIBER_T compiled
Obligatory SQL Fiddle.
This is shown in the syntax diagram in the documentation; you can have one or more attributes, and then one or more elements (including functions), but you can't mix them around. Notice which bits have loops, and where they loop back to.

Related

'SET' must be a type

I created the following types :
CREATE TYPE Adress AS Object (Street varchar2(50), PostalC number, Ville varchar2(50));
CREATE TYPE PhoneNumber;
A person can have a set of PhoneNumbers, when I try to create the Type Person :
CREATE TYPE Person AS Object (FirstName varchar2(50), LastName varchar2(50), Adr Adress, Mobile SET(PhoneNumber));
I get the following error :
Errors: TYPE PERSON Line/Col: 0/0 PL/SQL: Compilation unit analysis
terminated Line/Col: 1/90 PLS-00488: 'SET' must be a type
If you want an array of phone numbers for each person, define the phone number type as a nested table:
create or replace type address as object (
street varchar2(50), postalc number, ville varchar2(50)
);
/
create or replace type phonenumber as table of varchar2(20);
/
create or replace type person as object (
firstname varchar2(50), lastname varchar2(50),
adr address, mobile phonenumber
);

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') )

SQL error creating views (with nested tables)

I'm having trouble with a very basic example of creating a view.
These are the tables:
Create or replace type LEVEL_T as object(
Duties Varchar2(50),
Charge Number(10),
NO Date,
LIDZ Date
);
Create or replace type LEVELS_T as table of LEVEL_T;
Create table BOTS(
Bot_ID Number(10) constraint pk_bots primary key,
Name Varchar2(30),
Class Varchar2(30),
Start_date Date,
Levels LEVELS_T
) nested table Levels store as BOTS_LEVELS_COL;
Object:
Create or replace type OBJ_DATE as object(
Name Varchar2(30),
Class Varchar2(30),
Duties Varchar2(30),
Charge Number(10),
NO Date,
LIDZ Date,
MEMBER function WORKS(P_DATE in Date) return number
);
Create or replace type body OBJ_DATE as
MEMBER function WORKS(p_date in Date) return number is
begin
if ((p_date >= self.NO AND p_date < self.LIDZ) OR
(self.LIDZ IS NULL AND p_date >= self.NO)) then
return 1;
else
return 0;
end if;
end WORKS;
end;
And now i'm trying to make a view out of all this by:
Create or replace view VIEW_WORK of OBJ_DATE with object identifier(Name) as
Select A.Name, A.Class, B.Duties, B.Charge, B.NO, B.LIDZ
From BOTS A, table(A.Levels) B;
And keep getting an error starting at line 1: ORA-01730: invalid number of column names specified; 01730. 00000 - "invalid number of column names specified".
Was playing around with this for a bit and could only manage to get it to work when removing the part around "object identifier", but then calling the function WORKS (a bit ironical) doesn't when attempted to test the view. I'm completely stuck right now, so any tips on what am I doing wrong?

error- column not allowed here & too many values upon insert

I have this new problem after creating the Subscriber table of Subscriber_T type and its function i cant seem to insert any value in Subscrber table. The error, i keep getting is column not allowed here. But where???
These are the scripts and an insert record.
CREATE OR REPLACE TYPE Surnames_T AS OBJECT (
Surname varchar (20)
);
/
CREATE OR REPLACE TYPE listSurnames_T
AS Varray(4) of Surnames_T;
/
CREATE OR REPLACE TYPE listTels as object(
Tel number (12)
);
/
CREATE OR REPLACE TYPE listTels_T as Varray(5) of listTels;
/
CREATE OR REPLACE TYPE ADDRESS_T AS OBJECT (
NUM number (6),
STREET varchar (20),
TOWN varchar (20)
);
/
CREATE or replace type TAddress
as table of Address_T;
/
create or replace type Subscriber_T as object(
num_s number(6),
sName varchar(30),
surname listSurnames_T,
Adds TAddress,
DateOfBirth date,
phoneNo listTels_T,
member function Age return number
);
/
create table Subscribers of Subscriber_T(
CONSTRAINT subscriber_pk primary key (num_s)
)
nested table Adds store as Tab_Adds;
/
show errors
create or replace type body Subscriber_T as
member function Age return number is
calc_age number;
dob date;
diff date;
begin
select S.dateOfBirth into dob
from Subscribers S
where deref(S.num_s) = self
diff := sysdate - dob
calc_age := to_Char(diff, 'YYYY')
return cal_age;
end;
end;
/
show errors
insert into Subscribers values (34, Chloe, listSurnames_T(Surnames_T('Dave'), Surnames_T('Camille'), Surnames_T('Jones')),
TAddress(Address_T(10, 'ave Foch', 'Ravenwood'), Address_T(30, 'rue des pole', 'England')),
'10-11-1976',
listTels_T(listTels(5839550456), listTels(6834734567)));
I'm guessing the function is raising this problem but it shows no error on compilation.
Some issues:
You don't need to do a SELECT in the member function age.
You haven't quoted the name Chloe so its not a string.
You haven't converted the '10-11-1976' to a date.
You should not use VARCHAR as a datatype - use VARCHAR2 instead.
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE OR REPLACE TYPE Surnames_T AS OBJECT (
Surname varchar2 (20)
)
/
CREATE OR REPLACE TYPE listSurnames_T
AS Varray(4) of Surnames_T
/
CREATE OR REPLACE TYPE listTels as object(
Tel number (12)
)
/
CREATE OR REPLACE TYPE listTels_T as Varray(5) of listTels
/
CREATE OR REPLACE TYPE ADDRESS_T AS OBJECT (
NUM number (6),
STREET varchar2 (20),
TOWN varchar2 (20)
)
/
CREATE or replace type TAddress as table of Address_T
/
create or replace type Subscriber_T as object(
num_s number(6),
sName varchar2(30),
surname listSurnames_T,
Adds TAddress,
DateOfBirth date,
phoneNo listTels_T,
member function Age return number
)
/
create or replace type body Subscriber_T as
member function Age return number is
begin
return FLOOR( MONTHS_BETWEEN( SYSDATE, self.DateOfBirth )/12 );
end;
end;
/
create table Subscribers of Subscriber_T(
CONSTRAINT subscriber_pk primary key (num_s)
)
nested table Adds store as Tab_Adds
/
insert into Subscribers values (
34,
'Chloe',
listSurnames_T(
Surnames_T('Dave'),
Surnames_T('Camille'),
Surnames_T('Jones')
),
TAddress(
Address_T(10, 'ave Foch', 'Ravenwood'),
Address_T(30, 'rue des pole', 'England')
),
TO_DATE( '10-11-1976', 'DD-MM-YYYY' ),
listTels_T(
listTels(5839550456),
listTels(6834734567)
)
)
/
Query 1:
SELECT s.age() FROM subscribers s
Results:
| S.AGE() |
|---------|
| 37 |
Edit
I'm not sure why you are wrapping a VARCHAR2 in the Surnames_T type or a NUMBER in the ListTels type. You can omit these and just do:
CREATE OR REPLACE TYPE listSurnames_T
AS Varray(4) of VARCHAR2(20)
/
CREATE OR REPLACE TYPE listTels_T as Varray(5) of NUMBER(12)
/
and:
insert into Subscribers values (
34,
'Chloe',
listSurnames_T(
'Dave',
'Camille',
'Jones'
),
TAddress(
Address_T(10, 'ave Foch', 'Ravenwood'),
Address_T(30, 'rue des pole', 'England')
),
TO_DATE( '10-11-1976', 'DD-MM-YYYY' ),
listTels_T(
5839550456,
6834734567
)
)
/

oracle11g sql: Warning: Type created with compilation errors

I'm trying to create a supertype customer service and subtype agent and supervisor, so they can inherent values however when I try to run this in oracle sql: a message comes up
Warning: Type created with compilation errors.
What is wrong with the code below?
Create or replace type customer_s_type as object (
csID number,
csName varchar(15),
csType number ) NOT FINAL;
Create or replace type supervisor_type UNDER customer_s_type ( title varchar (10) );
Create or replace type agent_type UNDER customer_s_type (title varchar (10));
Create table supervisor of supervisor_type (
CONSTRAINT supervisor_PK PRIMARY KEY (csID));
Create table agent of agent_type (CONSTRAINT agent_PK PRIMARY KEY (csID));
create table customer_service(
csID number(10),
csType number(10),
constraint supervisor_pk primary key(csID) );
You can use show errors in SQL*Plus or SQL Developer, or select * from user_errors, to see the error details.
Since you've shown six commands and the warning is about one of the first three (since it refers to type), and they appear OK independently apart from the constraint pointed put in comments, it looks like the whole script is being imterpreted as one command. It depends on your client settings, but you probably just need to seperate the commands with a / to cause them to execute. Becuase types can include PL/SQL the ; isn't treated as a statement seperator. So:
Create or replace type customer_s_type as object (
csID number,
csName varchar(15),
csType number ) NOT FINAL;
/
Create or replace type supervisor_type UNDER customer_s_type ( title varchar (10) );
/
Create or replace type agent_type UNDER customer_s_type (title varchar (10));
/
Create table supervisor of supervisor_type (
CONSTRAINT supervisor_PK PRIMARY KEY (csID));
Create table agent of agent_type (CONSTRAINT agent_PK PRIMARY KEY (csID));
create table customer_service(
csID number(10),
csType number(10),
constraint customer_service_pk primary key(csID) );