r/programminghelp • • Sep 07 '23

SQL Need help with PL/SQL anonymous block.

Hi everyone. I'm currently making an anonymous block that will let me insert a number of rooms and for each room a set of N beds (1:N).

the code i'm writing is this one but i'm kinda stuck.i get the error PLS-00642, so i'm wondering if what i wan't to do can't be done or i'm doing it in the wrong way OR if making this in a stored procedure will solve this pls error... Thanks!

DECLARE
Type tableType is table of varchar2(50);
roomsDescr tableType;
roomsCode tableType;
rooms varchar2(4000) := 'room1;01|room2;02|room3;03|room4;04';

BEGIN DBMS_OUTPUT.ENABLE; WITH regxRooms as ( SELECT REGEXP_SUBSTR(rooms, '[|]+', 1, level) AS room FROM dual CONNECT BY REGEXP_SUBSTR(rooms, '[|]+', 1, level) IS NOT NULL ) SELECT (REGEXP_SUBSTR(regxRooms.room, '[;]+', 1, 1)) as DESCR , (REGEXP_SUBSTR(regxRooms.room, '[;]+', 1, 2)) as CODE INTO roomsDescr, roomsCode FROM regxRooms; for idx in roomsDescr.first..roomsDescr.last loop /Here i would insert them, each insert will have to return me the row ID./ DBMS_OUTPUT.PUT_LINE(roomsDescr(idx)); DBMS_OUTPUT.PUT_LINE(roomsCode(idx)); --insert into rooms returning rooms.id into roomId; --with the roomId, i'll need to do something like i did for the rooms and cycle through a "bed" "object" --made the same way as the rooms one (string separated by ; and |). /* Example, after inserting the room and inside this LOOP. for bedsIdx in bedsDescr.first..bedsdescr.last -> insert into beds roomId, bedsDescr(idx), bedsCode(idx) */ end loop; END;

edit: the code block messed up

edit: the code block keeps messing up... i'll add a screenshot

edit:pastebin link but i also fixed the error https://pastebin.com/hABFNFWf

2 Upvotes

6 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Sep 07 '23

I'm glad you figured it out! Thanks for posting an update on how you fixed it, too. If you can, edit the primary post with your fix details. Never know who that might help in the future.