【问题标题】:PLSQL how to store a result of a select statementPLSQL如何存储选择语句的结果
【发布时间】:2015-11-11 20:33:31
【问题描述】:

我需要根据一个参数从多个表中删除数据

问题是两个表相互关联,所以为了正确删除数据,我需要将 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 之间的某处存储选择查询的结果,稍后我可以在 delete 语句中使用该结果。 听起来,但我尝试了很多不同的方法,但似乎都不起作用。

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

提前谢谢你。

【问题讨论】:

标签: plsql plsqldeveloper


【解决方案1】:

您可以像这样简单地使用全局类型:

create or replace type myrec  is object (myid number);
create or replace type mytemp_collection is table of myrec;

declare 

  v_temp_collection mytemp_collection;
begin
  v_temp_collection  := mytemp_collection();

  select myrec (t.field_type_id ) bulk collect into v_temp_collection from fs_field_types t 
  where mod(t.field_type_id+1,3)=0; -- for example
  FOR i IN 1 .. v_temp_collection.count LOOP
    DBMS_OUTPUT.put_line(v_temp_collection(i).myid);
  End loop;  

 delete fs_field_types_back t where t.field_type_id in (select myid from table(v_temp_collection));


end;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    相关资源
    最近更新 更多