简体   繁体   English

检索并在SQL中存储某些ID,并在进一步的查询中使用它们

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


I have in an Oracle database the following situation: 我在Oracle数据库中有以下情况:

Table T1 with attributes ID and T2_ID. 具有属性ID和T2_ID的表T1。
Table T2 with attributes ID and T3_ID. 具有属性ID和T3_ID的表T2。
Table T3 with attributes ID and T4_ID. 具有属性ID和T4_ID的表T3。
Table T4 with attribute ID. 具有属性ID的表T4。

Each TX_ID is referring to the ID of the related table (example: T3_ID = ID in table T3). 每个TX_ID都引用相关表的ID(例如:T3_ID =表T3中的ID)。

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). 给定T1中的ID,我想编写一个SQL脚本来了解相关的T3_ID和T4_ID,将它们存储在单个变量中(仅包含检索到的值),并将这些值用于同一脚本中的DELETE / SELECT其他操作(例如,删除另一个T *表中所有具有T * _ID = T3_ID的行)。

Is there a way to do that in Oracle? 在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. 在PLSQL块中,使用类似的方法在变量v_t3_idv_t4_id获取数据,并将其用于delte / update语句中。

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. 如果数据集巨大,那么谷歌将介绍如何使用BULK COLLECT来执行相同的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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