0

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3074445
  • 3
  • 2
  • 6

2 Answers2

0

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.

Armunin
  • 986
  • 7
  • 18
  • ERROR at line 2: ORA-04127: single-row subquerry return more than one row – user3074445 Dec 06 '13 at 13:14
  • Updated SQL got all my rows deleted. Still not good :) Now it works like this - if there is any record with ID of L201, it deletes all of the rows. If there are none - nothing happens. – user3074445 Dec 06 '13 at 13:28
  • Yeah, didn't expect that ;) Happy, that @Wernfrieds solution worked for you. Don't forget to mark his answer as accepted. – Armunin Dec 06 '13 at 14:09
0

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;
Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110
  • I am still getting the error that subquerry line (Line 2) is not properly ended. Error at line 2: SQL command not properly ended. – user3074445 Dec 06 '13 at 13:34
  • If I do not include Table name in delete statement, I get error like this - Cannot perform DML on expression or on nested table view column – user3074445 Dec 06 '13 at 13:38