PLSQL如何存储select语句的结果

时间:2015-08-13 10:59:50

标签: plsql plsqldeveloper

我需要根据一个参数

删除许多表中的数据

问题是两个表彼此相关,所以为了正确删除数据我需要在某处存储id。

-- i would like to store temp data
-- this one is only for convienience to avoid repeating same select many times
create table ztTaryfa as select zt_taryfa from tw_zbiory_taryfy 
where 1=2;
-- this one is mandatory but I dont know how to make it work
Create table wnioskiId as select poli_wnio_id_wniosku from polisy 
where 1=2;


Begin

-- fill temp tables  
insert into ztTaryfa (
    select zt_taryfa from tw_zbiory_taryfy 
    where zt_zbior = :zbiorId);

  insert into wnioskiId (
    select poli_wnio_id_wniosku from polisy 
    where poli_taryfa_id in ztTaryfa);

  - regular deletion
  delete from POLISY_OT where ot_poli_id in (
    select poli_id from polisy 
    where poli_taryfa_id in ztTaryfa);
  commit;

  delete from DANE_RAPORTOWE where DR_RPU_ID in ( 
    select RPU_ID from ROZLICZ_PLIK_UBEZP where RPU_ROZLICZ_PLIK_ID in (
    select RP_ID from ROZLICZ_PLIK 
    where RP_ZBIOR_ID = :zbiorId ));
  commit;

  -- and here we go I need to delete data from POLISY first
  delete from POLISY where poli_taryfa_id in ztTaryfa;
  commit;

  -- but by doing it I lose ids which i need here, 
  -- so I have to store them somehow and use them here.
  delete from WNIOSKI where wnio_id in wnioskiId;
  commit;

End;

-- and now lets get rid off temp tables
drop table ztTaryfa;
commit;
drop table wnioskiId;
commit;

总结一下,我只需要知道如何在Begin和End之间存储一个select查询的结果,我稍后可以在delete语句中使用它。 听起来,但我尝试了很多不同的方法,似乎都没有用。

你在上面看到的只是脚本的1/3,所以我很想用一个参数来简单地使用它。

提前谢谢你。

1 个答案:

答案 0 :(得分:0)

您可以使用如此简单的全局类型:

Beta

根据您的业务更改select和where子句。