Deleting specific record from nested table Oracle DB

5k views Asked by At

I'm having problems deleting specific record from the table (ORACLE DB). I have a table with a nested table inside of it.

Table structure looks like this: where ML - nested table

Name, City, ML(Brand, Model, ID, Year, Price)

What I need to do is delete specific record with ID of 'L201'.

What I have tried so far:

SELECT B.ID FROM TABLE Dock A, Table(A.ML) B;

This is working giving me all the ID's.

Output:

ID
____
B201
S196
L201

This is not working when trying to delete the record:

DELETE FROM Dock
(SELECT B.ID FROM Dock A, Table(A.ML) B) C
WHERE C.ID = 'L201';

Getting error:

Line 2: SQL command not properly ended;

DELETE FROM TABLE
(SELECT D.ML FROM Dock D) E
WHERE E.ID = 'L201';

Throws an error:

single-row subquery returns more than one row

2

There are 2 answers

2
Wernfried Domscheit On BEST ANSWER

Maybe this one:

DELETE FROM 
   (SELECT A.Name, A.City, d.Brand, d.Model, d.ID, d.Year, d.Price 
   FROM Dock A, TABLE(ML) d)
WHERE ID = 'L201';

Update: Another trial before we gonna make it more advanced:

DELETE FROM Dock
WHERE ROWID =ANY (SELECT a.ROWID FROM Dock a, TABLE(ML) b WHERE b.ID = 'L201');

Update 2: If you prefer it more object-oriented, this one should work as well. At least I did not get any error.

CREATE OR REPLACE TYPE ML_TYPE AS OBJECT (
  brand VARCHAR2(100),
  ID VARCHAR2(20),
  MODEL VARCHAR2(20), 
  YEAR NUMBER, 
  Price NUMBER,
MAP MEMBER FUNCTION getID RETURN VARCHAR2,
CONSTRUCTOR FUNCTION ML_TYPE(ID IN VARCHAR2) RETURN SELF AS RESULT);


CREATE OR REPLACE TYPE BODY ML_TYPE IS   

CONSTRUCTOR FUNCTION ML_TYPE(ID IN VARCHAR2) RETURN SELF AS RESULT IS
-- Constructor to create dummy ML-Object which contains just an ID,
-- used for comparison
BEGIN
    SELF.ID := ID;
    RETURN;
END ML_TYPE;

MAP MEMBER FUNCTION getID RETURN VARCHAR2 IS
BEGIN
    RETURN SELF.ID;
END getID;
END;
/

CREATE OR REPLACE TYPE ML_TABLE_TYPE IS TABLE OF ML_TYPE;

CREATE TABLE Dock (Name VARCHAR2(20), City  VARCHAR2(20), ML ML_TABLE_TYPE) 
NESTED TABLE ML STORE AS ML_NT;

insert into Dock values ('A', 'NY', ML_TABLE_TYPE(
  ML_TYPE('brand1','L301','Model 2',2013, 1000),
  ML_TYPE('brand2','L101','Model 3',2013, 1000)));

insert into Dock values ('B', 'NY', ML_TABLE_TYPE(
  ML_TYPE('brand3','K301','Model 4',2014, 3000),
  ML_TYPE('brand4','K101','Model 5',2014, 3000)));

insert into Dock values ('A', 'NY', ML_TABLE_TYPE(
  ML_TYPE('brand5','K301','Model 8',2012, 2000),
  ML_TYPE('brand6','L201','Model 9',2012, 2000)));


DELETE FROM Dock WHERE ML_TYPE('L201') MEMBER OF ML;
3
Armunin On

After reading some literature I think I found the correct way to do this.
By defining a mapping-function for your object-type you can compare two nested tables directly.
Example:

-- creating the custom object type
create or replace type ml_type as object (
  brand varchar2(100),
  id varchar2(20),
  map member function sort_key return varchar2
);
-- creating the object type body and defining the map-function
create or replace type body ml_type as
    map member function sort_key return varchar2 is
    begin
        return self.brand || '|' || self.id;
    end;
end;
/
-- creating the nested table of custom type
create or replace type ml_tab as table of ml_type;

-- deleting from your table by comparing the nested-table elements
delete from dock where ml = (select ml from dock a, table(a.ml) b where b.id = 'L201');

In this example the map-functions is just returning the concatenated version of brand and id, but you can define it to what you want/need.