简体   繁体   中英

Retrieve and store certain ID in SQL and to use them in a further query


I have in an Oracle database the following situation:

Table T1 with attributes ID and T2_ID.
Table T2 with attributes ID and T3_ID.
Table T3 with attributes ID and T4_ID.
Table T4 with attribute ID.

Each TX_ID is referring to the ID of the related table (example: T3_ID = ID in table T3).

Given an ID in T1, I would like to write a SQL-script to know the related T3_ID and T4_ID, store them in single variables (containing only the retrieved values) and use those values for other operations of DELETE/SELECT in the same script (like DELETE all the rows from another T* table which have T*_ID = T3_ID).

Is there a way to do that in Oracle?

Inside a PLSQL block, use something like this to fetch data in variables v_t3_id and v_t4_id and use that in your delte/update statements.

select t3.id,t4.id into v_t3_id,v_t4_id 
from t1 
left join t2 on t1.t2_id=t2.id
left join t3 on t2.t3_id=t3.id
left join t4 on t3.t4_id=t4.id
where t1.id=<your value>

If data set is huge, then google on how to use BULK COLLECT to do the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM