?
Solved

SQL PLUS 4 table join by delimited list

Posted on 2003-02-26
2
Medium Priority
?
1,018 Views
Last Modified: 2013-12-12
I need to delete items in on table based on values linked thru 3 other tables.  I tried this, which was too slow:

DELETE  FROM PP_REQUIREMENT_DETAILS      
WHERE RESOURCE_REQUIREMENT_ID IN
     (SELECT rr.RESOURCE_REQUIREMENT_ID
     FROM    PP_RESOURCE_REQUIREMENTS        rr      
     WHERE     rr.Task_ID                          IN        
                                                               (SELECT TASK_ID
                                                               FROM PA_TASKS
                                                               WHERE PROJECT_ID IN
                                                                          (SELECT PROJECT_ID
                                                                       FROM PA_PROJECTS
                                                                       WHERE PROJECT_NUMBER IN
               (     '20519',
                    '20307',
                    '20287',
                    '1747.04',
                    '1747.03',))))


Then I tried this:

DELETE  FROM PP_REQUIREMENT_DETAILS WHERE RESOURCE_REQUIREMENT_ID IN (SELECT rr.RESOURCE_REQUIREMENT_ID FROM PP_RESOURCE_REQUIREMENTS rr WHERE rr.Task_ID IN (SELECT TASK_ID FROM PA_TASKS WHERE PROJECT_ID IN (SELECT PROJECT_ID FROM PA_PROJECTS WHERE PROJECT_NUMBER = '20519')))/
DELETE  FROM PP_REQUIREMENT_DETAILS WHERE RESOURCE_REQUIREMENT_ID IN (SELECT rr.RESOURCE_REQUIREMENT_ID FROM PP_RESOURCE_REQUIREMENTS rr WHERE rr.Task_ID IN (SELECT TASK_ID FROM PA_TASKS WHERE PROJECT_ID IN (SELECT PROJECT_ID FROM PA_PROJECTS WHERE PROJECT_NUMBER = '20307')))/
DELETE  FROM PP_REQUIREMENT_DETAILS WHERE RESOURCE_REQUIREMENT_ID IN (SELECT rr.RESOURCE_REQUIREMENT_ID FROM PP_RESOURCE_REQUIREMENTS rr WHERE rr.Task_ID IN (SELECT TASK_ID FROM PA_TASKS WHERE PROJECT_ID IN (SELECT PROJECT_ID FROM PA_PROJECTS WHERE PROJECT_NUMBER = '20287')))/
DELETE  FROM PP_REQUIREMENT_DETAILS WHERE RESOURCE_REQUIREMENT_ID IN (SELECT rr.RESOURCE_REQUIREMENT_ID FROM PP_RESOURCE_REQUIREMENTS rr WHERE rr.Task_ID IN (SELECT TASK_ID FROM PA_TASKS WHERE PROJECT_ID IN (SELECT PROJECT_ID FROM PA_PROJECTS WHERE PROJECT_NUMBER = '1747.04')))/
DELETE  FROM PP_REQUIREMENT_DETAILS WHERE RESOURCE_REQUIREMENT_ID IN (SELECT rr.RESOURCE_REQUIREMENT_ID FROM PP_RESOURCE_REQUIREMENTS rr WHERE rr.Task_ID IN (SELECT TASK_ID FROM PA_TASKS WHERE PROJECT_ID IN (SELECT PROJECT_ID FROM PA_PROJECTS WHERE PROJECT_NUMBER = '1747.03')))/


This is multiple delete statements for each value.  The slash should probably a semicolon.  But, I can't get it to work in SQL PLUS.  I need the correct syntax and options to run multiple statements in a block like this in SQL PLUS.  

Or - perhaps there's a better way.
0
Comment
Question by:Jon_Raymond
2 Comments
 
LVL 8

Accepted Solution

by:
Danielzt earned 800 total points
ID: 8029628
2 ways for you:

1:(supposed way) save these comands into a file, let's say  DEL_COM.sql.
 then you can start sql*plus,
 SQL>@DEL_COM.sql

DELETE  FROM PP_REQUIREMENT_DETAILS WHERE RESOURCE_REQUIREMENT_ID IN (SELECT rr.RESOURCE_REQUIREMENT_ID FROM PP_RESOURCE_REQUIREMENTS rr WHERE rr.Task_ID IN (SELECT TASK_ID FROM PA_TASKS WHERE PROJECT_ID IN (SELECT PROJECT_ID FROM PA_PROJECTS WHERE PROJECT_NUMBER = '20519')));
/
DELETE  FROM PP_REQUIREMENT_DETAILS WHERE RESOURCE_REQUIREMENT_ID IN (SELECT rr.RESOURCE_REQUIREMENT_ID FROM PP_RESOURCE_REQUIREMENTS rr WHERE rr.Task_ID IN (SELECT TASK_ID FROM PA_TASKS WHERE PROJECT_ID IN (SELECT PROJECT_ID FROM PA_PROJECTS WHERE PROJECT_NUMBER = '20307')));
/
DELETE  FROM PP_REQUIREMENT_DETAILS WHERE RESOURCE_REQUIREMENT_ID IN (SELECT rr.RESOURCE_REQUIREMENT_ID FROM PP_RESOURCE_REQUIREMENTS rr WHERE rr.Task_ID IN (SELECT TASK_ID FROM PA_TASKS WHERE PROJECT_ID IN (SELECT PROJECT_ID FROM PA_PROJECTS WHERE PROJECT_NUMBER = '20287')));
/
DELETE  FROM PP_REQUIREMENT_DETAILS WHERE RESOURCE_REQUIREMENT_ID IN (SELECT rr.RESOURCE_REQUIREMENT_ID FROM PP_RESOURCE_REQUIREMENTS rr WHERE rr.Task_ID IN (SELECT TASK_ID FROM PA_TASKS WHERE PROJECT_ID IN (SELECT PROJECT_ID FROM PA_PROJECTS WHERE PROJECT_NUMBER = '1747.04')));
/
DELETE  FROM PP_REQUIREMENT_DETAILS WHERE RESOURCE_REQUIREMENT_ID IN (SELECT rr.RESOURCE_REQUIREMENT_ID FROM PP_RESOURCE_REQUIREMENTS rr WHERE rr.Task_ID IN (SELECT TASK_ID FROM PA_TASKS WHERE PROJECT_ID IN (SELECT PROJECT_ID FROM PA_PROJECTS WHERE PROJECT_NUMBER = '1747.03')));
/

2: start SQL*PLUS
sql> begin
2  ..... enter all your commands here,line by line, no slash ....
3
 DELETE  FROM PP_REQUIREMENT_DETAILS WHERE RESOURCE_REQUIREMENT_ID IN (SELECT rr.RESOURCE_REQUIREMENT_ID FROM PP_RESOURCE_REQUIREMENTS rr WHERE rr.Task_ID IN (SELECT TASK_ID FROM PA_TASKS WHERE PROJECT_ID IN (SELECT PROJECT_ID FROM PA_PROJECTS WHERE PROJECT_NUMBER = '20519')));
DELETE  FROM PP_REQUIREMENT_DETAILS WHERE RESOURCE_REQUIREMENT_ID IN (SELECT rr.RESOURCE_REQUIREMENT_ID FROM PP_RESOURCE_REQUIREMENTS rr WHERE rr.Task_ID IN (SELECT TASK_ID FROM PA_TASKS WHERE PROJECT_ID IN (SELECT PROJECT_ID FROM PA_PROJECTS WHERE PROJECT_NUMBER = '20307')));
...

end;
/

(pay attention the laste two lines)


0
 
LVL 4

Expert Comment

by:stemu2000
ID: 8029874

direct joins are better in most cases:


DELETE PP_REQUIREMENT_DETAILS rd
WHERE exists (
    (SELECT  1
     FROM    PP_RESOURCE_REQUIREMENTS  rr,
             PA_TASKS t,
             PA_PROJECTS pap
     WHERE   rd.RESOURCE_REQUIREMENT_ID = rr.RESOURCE_REQUIREMENT_ID
      and    rr.Task_ID = t.TASK_ID
      and    t.PROJECT_ID = pap.PROJECT_ID
      and    pap.PROJECT_NUMBER IN
              (    '20519',
                   '20307',
                   '20287',
                   '1747.04',
                   '1747.03',))))


Cheers, Stefan
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.
Suggested Courses
Course of the Month8 days, 21 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question