There are 3 tables which has relations to each other so I am not able to just drop the rows.
I wrote a query which declares records, loop through 1st table then use the _id to get the record for second table then the 3rd table.
But after getting all the id from each table, I need to drop those id from 3rd table, then 2nd table then 1st table.
I am not able to declare and store the rows found into a variable.
DO $$
DECLARE
firstTableRecord record;
-- tried to declare something like
-- firstTableRecord record[]; or firstTableRecord [];
secondTableRecord record;
thirdTableRecord record;
BEGIN
-- Get FirstTable Records
FOR firstTableRecord IN
SELECT _id FROM public."FirstTable"
LOOP
RAISE NOTICE 'firstTableRecord: %', firstTableRecord;
-- Using the Ids from FirstTable get records from SecondTable
FOR secondTableRecord IN
select _id from public."SecondTable" where _id = firstTableRecord._id
LOOP
RAISE NOTICE 'secondTableRecord: %', secondTableRecord;
-- Using the Ids from SecondTable get records from ThirdTable
FOR thirdTableRecord IN
select _id from public."ThirdTable" where _id = firstTableRecord._id
LOOP
RAISE NOTICE 'thirdTableRecord: %', thirdTableRecord;
END LOOP;
END LOOP;
END LOOP;
-- remove all record found in third table
-- remove all record found in second table
-- remove all record found in first table
END$$;
Am I thinking / doing this the wrong direction that there's an easier way or how can this be done?
Thanks in advance for any suggestions and advice;