0
Table1 Table2 Table3
Req_id Req_id env_id=1
env_id(FK) env_id=2

Ignoring other fields...Each req_id in Table 2 has an env_id and it gets this env_id from Table 3.

If a req_id in Table 2 has a record with EVERY env_id in Table 3, I want to delete that req_id from Table 1.

Since the env_id's in Table 3 will be different in every environment where I will run this script. So, Delete script needs to be written dynamically. Thank you for any help.

   BEGIN
       FOR a
          IN (SELECT ENV_ID FROM Table3 t3)
            LOOP
              BEGIN
               DELETE FROM Table1 t1 WHERE t1.ID IN
               (SELECT t1.ID FROM Table1 t1
                INNER JOIN Table2 t2 ON t2.REQ_ID = t1.REQ_ID
                INNER JOIN Table3 t3 ON t3.ENV_ID = t2.ENV_ID
                AND t2.ENV_ID=a.ENV_ID)
              END;
           END LOOP;
      COMMIT;
    END;

1 Answers1

0

If a req_id in Table 2 has a record with EVERY env_id in Table 3, I want to delete that req_id from Table 1.

I believe you're describing relational division here. In sql, it's not so obvious how to write/read queries for this operation so there are a lot of questions/answers in stackoverflow and elsewhere.

Some examples:

The rest of your requirements should just be joining through to the correct tables for the delete.

Chris D
  • 102
  • 1
  • 7