Invalid Identifier on Where Clause PL/SQL - sql

Trying to perform a query on data in a table. The query is 'Give album title, album release date and album price of all Neil Young’s albums released after 1st January 2015.'
The relevant structure of the table (excluding other parts) is as follows:
create or replace type artist_type as object
(artistName varchar(50),
artistRole varchar(25))
/
create type artist_array_type
as varray(5) of artist_type
/
create or replace type album_type as object
(albumTitle varchar(50),
albumPlaytime number(3), -- minutes
albumReleaseDate date,
albumGenre varchar(15),
albumPrice number(9,2),
albumTracks number(2),
albumArtists artist_array_type,
albumReviews review_table_type,
member function discountPrice return number,
member function containsText (pString1 varchar2, pString2 varchar2) return integer)
not instantiable not final
/
The below is my current query,
select a.albumtitle, a.albumreleasedate, a.albumprice
from albums a
where a.albumartists.artist_type.artistname = 'Neil Young'
and a.albumreleasedate >= '1-Jan-2015';
however upon attempting to execute it in Oracle SQL Developer I get the error:
ORA-00904: "A"."ALBUMARTISTS"."ARTIST_TYPE"."ARTISTNAME": invalid identifier
00000 - "%s: invalid identifier"

Related

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?

How can I fix this “SQL Statement ignored” error? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have the following small function that does not compile:
CREATE OR REPLACE PROCEDURE insertarVentas(ID NUMBER, IDCliente NUMBER, nombre VARCHAR2, calle VARCHAR2, poblacion VARCHAR2, cp NUMBER, provincia VARCHAR2, dni VARCHAR2,
telef1 VARCHAR2, telef2 VARCHAR2, telef3 VARCHAR2, fechaventa date, numerolinea NUMBER, IDProducto NUMBER, descripcion VARCHAR2, pvp NUMBER, stockactual NUMBER, cantidad NUMBER) AS
BEGIN
INSERT INTO TABLA_VENTAS VALUES (ID, TIP_CLIENTE(IDCliente, nombre, TIP_DIRECCION(calle, poblacion, cp, provincia), dni, TIP_TELEFONOS(telef1, telef2, telef3)), fechaventa,
TIP_LINEAS_VENTA(numerolinea, TIP_PRODUCTO(IDProducto, descripcion, pvp, stockactual), cantidad));
END insertarVentas;
The compiler gives me the following errors:
Error at line 4: PL/SQL: SQL Statement ignored
2. telef1 VARCHAR2, telef2 VARCHAR2, telef3 VARCHAR2, fechaventa date, numerolinea NUMBER, IDProducto NUMBER, descripcion VARCHAR2, pvp NUMBER, stockactual NUMBER, cantidad NUMBER) AS
3. BEGIN
4. INSERT INTO TABLA_VENTAS VALUES (ID, TIP_CLIENTE(IDCliente, nombre, TIP_DIRECCION(calle, poblacion, cp, provincia), dni, TIP_TELEFONOS(telef1, telef2, telef3)), fechaventa,
5. TIP_LINEAS_VENTA(numerolinea, TIP_PRODUCTO(IDProducto, descripcion, pvp, stockactual), cantidad));
6. END insertarVentas;
This is an insert into a object table, this is the code that table
CREATE TABLE TABLA_VENTAS OF TIP_VENTA(
IDVENTA PRIMARY KEY
)NESTED TABLE LINEAS STORE AS TABLA_LINEAS;
CREATE TYPE TIP_VENTA AS OBJECT(
IDVENTA NUMBER,
IDCLIENTE REF TIP_CLIENTE,
CREATE TYPE TIP_CIENTE AS OBJECT(
IDCLIENTE NUMBER,
NOMBRE VARCHAR2(50),
DIREC TIP_DIRECCION,
CREATE TYPE TIP_DIRECCION AS OBJECT(
CALLE VARCHAR2(50),
POBLACION VARCHAR2(50),
CODPOSTAL NUMBER(5),
PROVINCIA VARCHAR2(40)
);
NIF VARCHAR2(9),
TELEF TIP_TELEFONOS
CREATE TYPE TIP_TELEFONOS AS VARRAY(3) OF VARCHAR2(15);
);
FECHAVENTA DATE,
LINEAS TIP_LINEAS_VENTA,
CREATE TYPE TIP_LINEAVENTA AS OBJECT(
NUMEROLINEA NUMBER,
IDPRODUCTO REF TIP_PRODUCTO,
CREATE TYPE TIP_PRODUCTO AS OBJECT(
IDPRODUCTO NUMBER,
DESCRIPCION VARCHAR2(80),
PVP NUMBER,
STOCKACTUAL NUMBER
);
CANTIDAD NUMBER
);
CREATE TYPE TIP_LINEAS_VENTA AS TABLE OF TIP_LINEAVENTA;
);
What is causing this error and how can I fix it?
The PL/SQL: SQL Statement ignored message is usually just the top of the error stack and a following line will indicate what the actual issue is.
Having untangled your object creation statements and got them in an order that lets them all compile (and fixed at least one typo, and guess that a dangling comma was also a typo), creating your procedure gets:
LINE/COL ERROR
-------- ------------------------------------
4/1 PL/SQL: SQL Statement ignored
4/13 PL/SQL: ORA-00947: not enough values
You've defined TIP_VENTA with five fields:
CREATE TYPE TIP_VENTA AS OBJECT(
IDVENTA NUMBER,
IDCLIENTE REF TIP_CLIENTE,
FECHAVENTA DATE,
LINEAS TIP_LINEAS_VENTA,
CANTIDAD NUMBER
);
But the object you create during your insert only has four values (reformatted to make it a bit more readable):
INSERT INTO TABLA_VENTAS VALUES (
ID,
TIP_CLIENTE(
IDCliente,
nombre,
TIP_DIRECCION(calle, poblacion, cp, provincia),
dni,
TIP_TELEFONOS(telef1, telef2, telef3)
),
fechaventa,
TIP_LINEAS_VENTA(
numerolinea,
TIP_PRODUCTO(IDProducto, descripcion, pvp, stockactual),
cantidad
)
);
You've got cantidad inside the TIP_LINEAS_VENTA() constructor call, instead of after it:
INSERT INTO TABLA_VENTAS VALUES (
ID,
TIP_CLIENTE(
IDCliente,
nombre,
TIP_DIRECCION(calle, poblacion, cp, provincia),
dni,
TIP_TELEFONOS(telef1, telef2, telef3)
),
fechaventa,
TIP_LINEAS_VENTA(
numerolinea,
TIP_PRODUCTO(IDProducto, descripcion, pvp, stockactual)
),
cantidad
);
But that now gets:
LINE/COL ERROR
-------- ------------------------------------------------------------------
9/1 PL/SQL: SQL Statement ignored
20/5 PL/SQL: ORA-00932: inconsistent datatypes: expected UDT got NUMBER
The fourth field of the TIP_VENTA is type TIP_LINEAS_VENTA, so you need to have a TIP_LINEAVENTA within that table constructor:
TIP_LINEAS_VENTA(
TIP_LINEAVENTA(
numerolinea,
TIP_PRODUCTO(IDProducto, descripcion, pvp, stockactual)
)
Which then gets:
LINE/COL ERROR
-------- -----------------------------------------------------------------------------------------------------------------
9/1 PL/SQL: SQL Statement ignored
22/7 PL/SQL: ORA-00932: inconsistent datatypes: expected X.TIP_PRODUCTO got REF X.TIP_PRODUCTO
... because you're using actual objects instead of REFs to them, as the other type definitions expect. You'll need to decide whether to make them actual objects or fix the REFs...
I think the issue is that you're trying to insert into a table of an object something that isn't an object.
I've split out your object type populations into variables (rather than leaving them nested inside the insert statement as you did) which hopefully enables you to see what it is you're actually trying to insert:
create or replace procedure insertarventas (id number,
idcliente number,
nombre varchar2,
calle varchar2,
poblacion varchar2,
cp number,
provincia varchar2,
dni varchar2,
telef1 varchar2,
telef2 varchar2,
telef3 varchar2,
fechaventa date,
numerolinea number,
idproducto number,
descripcion varchar2,
pvp number,
stockactual number,
cantidad number)
as
v_tip_venta tip_venta;
v_tip_cliente tip_cliente;
v_tip_direccion tip_direccion;
v_tip_telefonos tip_telefonos;
v_tip_lineaventa tip_lineaventa;
v_tip_lineas_venta tip_lineas_venta;
v_tip_producto tip_producto;
begin
v_tip_direccion := tip_direccion (calle,
poblacion,
cp,
provincia);
v_tip_telefonos := tip_telefonos (telef1,
telef2,
telef3);
v_tip_cliente := tip_cliente (idcliente,
nombre,
v_tip_direccion,
dni,
v_tip_telefonos);
v_tip_producto := tip_producto (idproducto,
descripcion,
pvp,
stockactual);
v_tip_lineaventa := tip_lineaventa (numerolinea,
v_tip_producto,
cantidad);
v_tip_lineas_venta := tip_lineas_venta (v_tip_lineaventa);
-- this is the step you were missing
v_tip_venta := tip_venta (id,
v_tip_cliente,
fechaventa,
v_tip_lineas_venta);
insert into tabla_ventas
values (v_tip_venta);
end insertarventas;
/
N.B. untested.
Ok, the presence of the REFs in the object type descriptions is what is causing the problems. However, I don't think it's necessary to use REF (not to mention that I couldn't work out how to make the code work with them! *{;-) ), so here is a working test case without the REFs:
drop procedure insertarventas;
drop table tabla_ventas;
drop type tip_venta;
drop type tip_lineas_venta;
drop type tip_lineaventa;
drop type tip_producto;
drop type tip_cliente;
drop type tip_telefonos;
drop type tip_direccion;
create type tip_direccion as object (calle varchar2(50),
poblacion varchar2(50),
codpostal number(5),
provincia varchar2(40));
create type tip_telefonos as varray(3) of varchar2(15);
create type tip_cliente as object (idcliente number,
nombre varchar2(50),
direc tip_direccion,
nif varchar2(9),
telef tip_telefonos);
create type tip_producto as object (idproducto number,
descripcion varchar2(80),
pvp number,
stockactual number);
create type tip_lineaventa as object (numerolinea number,
idproducto tip_producto,
cantidad number);
create type tip_lineas_venta as table of tip_lineaventa;
create type tip_venta as object (idventa number,
idcliente tip_cliente,
fechaventa date,
lineas tip_lineas_venta);
create table tabla_ventas of tip_venta (idventa primary key)
nested table lineas store as tabla_lineas;
create or replace procedure insertarventas (id number,
idcliente number,
nombre varchar2,
calle varchar2,
poblacion varchar2,
cp number,
provincia varchar2,
dni varchar2,
telef1 varchar2,
telef2 varchar2,
telef3 varchar2,
fechaventa date,
numerolinea number,
idproducto number,
descripcion varchar2,
pvp number,
stockactual number,
cantidad number)
as
v_tip_venta tip_venta;
v_tip_cliente tip_cliente;
v_tip_direccion tip_direccion;
v_tip_telefonos tip_telefonos;
v_tip_lineaventa tip_lineaventa;
v_tip_lineas_venta tip_lineas_venta;
v_tip_producto tip_producto;
begin
v_tip_direccion := tip_direccion (calle,
poblacion,
cp,
provincia);
v_tip_telefonos := tip_telefonos (telef1,
telef2,
telef3);
v_tip_cliente := tip_cliente (idcliente,
nombre,
v_tip_direccion,
dni,
v_tip_telefonos);
v_tip_producto := tip_producto (idproducto,
descripcion,
pvp,
stockactual);
v_tip_lineaventa := tip_lineaventa (numerolinea,
v_tip_producto,
cantidad);
v_tip_lineas_venta := tip_lineas_venta (v_tip_lineaventa);
-- this is the step you were missing
v_tip_venta := tip_venta (id,
v_tip_cliente,
fechaventa,
v_tip_lineas_venta);
insert into tabla_ventas
values (v_tip_venta);
end insertarventas;
/

error- declaration is incomplete or malformed

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.

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