Search code examples
sqloracleprocedureuser-defined-types

sql oracle. procedure cursor loops


I have create the following tables and types....

CREATE TYPE  ACTOR_QUOTE_TYPE AS OBJECT ( 
Movie_Title  CHAR(36),
Year NUMBER,
Role  CHAR(36),
Quote CHAR(255)
)
/

CREATE TYPE AQ_NT AS TABLE OF  ACTOR_QUOTE_TYPE
/

CREATE TABLE ACTOR_QUOTES (
ACTORID CHAR(5),
QUOTES  AQ_NT
)  NESTED TABLE QUOTES STORE AS ACTOR_QUOTES_NT
/

I need to create an.....

A PL/SQL procedure called INIT_ACTOR_QUOTES with no parameters that:

Reads ALL the ACTORIDs from the ACTOR table and INSERTs them into the ACTORID attribute for each row the ACTOR_QUOTES table (the tables have the same cardinality) and at the same time INSERTs the following initial values into the first row only of the QUOTES nested table into each row of the ACTOR_QUOTES table;

(Movie_Title, Year, Role, Quote) are set respectively to (' ',NULL ,' ', ' ')

Also and at the same time immediately after each INSERT use DELETE to delete ALL the rows from the nested table in each row belonging to each ACTORID in the ACTOR_QUOTES table.

I'm getting a compilation error with the code....

CREATE OR REPLACE PROCEDURE INIT_ACTOR_QUOTES 
AS
CURSOR actorID_cursor IS
SELECT actorID FROM Actor;
BEGIN 

FOR row IN actorID_cursor LOOP
INSERT actorID INTO ACTOR_QUOTES;

INSERT INTO actor_Quotes_NT VALUES ('', NULL, ' ', '');
DELETE (*) FROM actor_Quotes_NT ('', NULL, ' ', ''); 
END LOOP; 
END INIT_ACTOR_QUOTES ;

/ 
Warning: Procedure created with compilation errors.






SQL> show errors;
Errors for PROCEDURE INIT_ACTOR_QUOTES:

 LINE/COL ERROR
 -------- -----------------------------------------------------------------
 7/2      PL/SQL: SQL Statement ignored    
 7/9      PL/SQL: ORA-00925: missing INTO keyword
 9/2      PL/SQL: SQL Statement ignored
 9/10     PL/SQL: ORA-00928: missing SELECT keyword
 SQL> 

Any Help Please....


Solution

  • First error: when inserting into a table we have to insert values into all the columns or otherwise just specify the ones we are populating.

    Second error: when referencing a column in a cursor we have to reference the variable we're fetching data into i.e. row in your ill-named example.

    Third: when populating a user-defined type we have to name the type in the assignment.

    Fourth: when populating a nested table we have to name both it and the object type it collects.

    Fifth: a nested table is a column on the table and so should be assigned in the INSERT statement, or subsequently with an UPDATE.

    So what you really want is something which looks like this:

     insert into ACTOR_QUOTES
        (actorid, quote)s
     values
        ( row.actorid
          , AQ_NT ( 
               ACTOR_QUOTE_TYPE  
                 ('Dirty Harry', 1970, 'Inspector Callahan', 'Do you feel lucky punk?')
              , ACTOR_QUOTE_TYPE  
                 ('Sudden Impact', 1983, 'Inspector Callahan', 'Make my day')
            )
       );
    

    By the by, on the topic of ill-named variables, don't call anything AQ. That is a recognized abbreviation for the Oracle Advance Queueing feature, so it's just going to cause a world of confusion.