I'm currently trying to figure out a question on my assignment. I've never worked with arrays before but I've done collections, triggers, functions, procedures, and cursors. I'm not asking for the answer but rather some help on this as I'm confused on how to approach it.
Declare a record where you keep record of LOCATIONS table (LOCATION_ID, STREET_ADDRESS, POSTAL_CODE, CITY, STATE_PROVINCE and COUNTRY_ID) and an operation.
Declare an array where you keep location records as elements.
The part about the operations also confuses me because I have done some work where you would use the operators in triggers but it didn't involve an array:
BEGIN
IF INSERTING THEN
INSERT INTO carlog
VALUES ('INSERT',user, SYSDATE, UPPER(:NEW.serial), UPPER(:NEW.make),
UPPER(:NEW.model), UPPER(:NEW.color));
END IF;
IF UPDATING THEN
INSERT INTO carlog
VALUES ('UPDATE',user, SYSDATE,:old.serial, old.make,
old.model, old.color);
END IF;
IF DELETING THEN
INSERT INTO carlog
VALUES ('DELETE',user, SYSDATE,:old.serial, old.make,
old.model, old.color);
END IF;
END;
An array is just a type of collection. If you look at the documentation here: https://docs.oracle.com/cd/B28359_01/appdev.111/b28370/collections.htm#LNPLS00501
You can see that an "Associative Array" is simply a PL/SQL collection type.
As far as the "Operation" is concerned, my understanding based on the spec is that it is purely an attribute of the location record itself. I would make something similar to the below:
DECLARE
--declare your location record based on the spec
TYPE location IS RECORD (
location_id integer,
operation VARCHAR2(1)
--additional values from the spec would go here
);
--declare an array as a table of the location type
TYPE location_array IS TABLE OF location INDEX BY BINARY_INTEGER;
--two variables to hold the data
example_location location;
locations location_array;
BEGIN
--start building your location record for the location you want to modify
example_location.location_id := 1;
example_location.operation := 'T';
--..... additional values here
--add the location to the array
locations(locations.count + 1) := example_location;
--repeat the above for any other locations, or populate these some other way
--loop through the locations
FOR i IN locations.first..locations.last
LOOP
--decide the logic based on the operation, could just as easily use your if logic here
CASE locations(i).operation
WHEN 'U' THEN
dbms_output.put_line('your update logic here');
WHEN 'I' THEN
dbms_output.put_line('your insert logic here');
WHEN 'D' THEN
dbms_output.put_line('your delete logic here');
ELSE
dbms_output.put_line('other operations');
END CASE;
END LOOP;
END;
You would need to adapt the above to suit your needs, adding in the relevant logic, messages, commits, error handling etc.