2015-08-13 99 views
0

我需要删除基于一个参数从多个表中的数据PLSQL如何存储select语句的结果

的问题是,两个表是如此,才能正常删除的数据,我需要彼此相关存储身份证的地方。

-- 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,所以我很想让这一切都很简单,只需要一个参数。

非常感谢您。

+0

阅读精美手册:[使用SELECT INTO语句的查询结果集处理](http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/static.htm#LNPLS551)。 – user272735

回答

0

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

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; 

变化选择并在根据您的业务条款。

相关问题