我想插入表格。我是新手并且正在努力学习。我正在使用SQLDEV。我相信它必须是嵌套循环的一段时间,但我无法弄明白。该表适用于员工及其家属。我只是想设置SEQ_NO。例如,BREL_CODE为'E'SEQ_NO的每个人都应该设置为1.之后,数字应该增加1,直到达到新的PIDM,然后循环应重新开始并重复该过程。例如...... SEQ_NO列中的前9行应为1,2,3,4,2,1,2,1,1 ......
Sample Data:
(PZRBENE_PIDM,PZRBENE_SEQ_NO,PZRBENE_BENE_PIDM,PZRBENE_BENE_LAST_NAME,PZRBENE_BENE_FIRST_NAME,PZRBENE_BENE_MI,PZRBENE_BREL_CODE,PZRBENE_SEX_IND,PZRBENE_BIRTH_DATE,PZRBENE_SSN,PZRBENE_MEMBER_TYPE,PZRBENE_DEL_IND) values (33159,null,null,'Regr','Ken',null,'E','F',to_date('09-FEB-68','DD-MON-RR'),'111111111','N','N');
Insert into PZRBENE (PZRBENE_PIDM,PZRBENE_SEQ_NO,PZRBENE_BENE_PIDM,PZRBENE_BENE_LAST_NAME,PZRBENE_BENE_FIRST_NAME,PZRBENE_BENE_MI,PZRBENE_BREL_CODE,PZRBENE_SEX_IND,PZRBENE_BIRTH_DATE,PZRBENE_SSN,PZRBENE_MEMBER_TYPE,PZRBENE_DEL_IND) values (33159,null,null,'Reg','Den',null,'H','M',to_date('24-DEC-71','DD-MON-RR'),'222222222','D','N');
Insert into PZRBENE (PZRBENE_PIDM,PZRBENE_SEQ_NO,PZRBENE_BENE_PIDM,PZRBENE_BENE_LAST_NAME,PZRBENE_BENE_FIRST_NAME,PZRBENE_BENE_MI,PZRBENE_BREL_CODE,PZRBENE_SEX_IND,PZRBENE_BIRTH_DATE,PZRBENE_SSN,PZRBENE_MEMBER_TYPE,PZRBENE_DEL_IND) values (33159,null,null,'Regr','Lev',null,'C','M',to_date('12-NOV-10','DD-MON-RR'),'333333333','D','N');
Insert into PZRBENE (PZRBENE_PIDM,PZRBENE_SEQ_NO,PZRBENE_BENE_PIDM,PZRBENE_BENE_LAST_NAME,PZRBENE_BENE_FIRST_NAME,PZRBENE_BENE_MI,PZRBENE_BREL_CODE,PZRBENE_SEX_IND,PZRBENE_BIRTH_DATE,PZRBENE_SSN,PZRBENE_MEMBER_TYPE,PZRBENE_DEL_IND) values (33159,null,null,'Reger','Ja',null,'C','M',to_date('12-AUG-05','DD-MON-RR'),'444444444','D','N');
Insert into PZRBENE (PZRBENE_PIDM,PZRBENE_SEQ_NO,PZRBENE_BENE_PIDM,PZRBENE_BENE_LAST_NAME,PZRBENE_BENE_FIRST_NAME,PZRBENE_BENE_MI,PZRBENE_BREL_CODE,PZRBENE_SEX_IND,PZRBENE_BIRTH_DATE,PZRBENE_SSN,PZRBENE_MEMBER_TYPE,PZRBENE_DEL_IND) values (33161,null,null,'Lace','Hary','C','H','M',to_date('15-SEP-47','DD-MON-RR'),'555555555','D','N');
Insert into PZRBENE (PZRBENE_PIDM,PZRBENE_SEQ_NO,PZRBENE_BENE_PIDM,PZRBENE_BENE_LAST_NAME,PZRBENE_BENE_FIRST_NAME,PZRBENE_BENE_MI,PZRBENE_BREL_CODE,PZRBENE_SEX_IND,PZRBENE_BIRTH_DATE,PZRBENE_SSN,PZRBENE_MEMBER_TYPE,PZRBENE_DEL_IND) values (33161,null,null,'Love','Rose','M','E','F',to_date('10-FEB-51','DD-MON-RR'),'666666666','N','N');
Insert into PZRBENE (PZRBENE_PIDM,PZRBENE_SEQ_NO,PZRBENE_BENE_PIDM,PZRBENE_BENE_LAST_NAME,PZRBENE_BENE_FIRST_NAME,PZRBENE_BENE_MI,PZRBENE_BREL_CODE,PZRBENE_SEX_IND,PZRBENE_BIRTH_DATE,PZRBENE_SSN,PZRBENE_MEMBER_TYPE,PZRBENE_DEL_IND) values (33163,null,null,'Wilon','La','E','C','F',to_date('12-FEB-02','DD-MON-RR'),'777777777','D','N');
Insert into PZRBENE (PZRBENE_PIDM,PZRBENE_SEQ_NO,PZRBENE_BENE_PIDM,PZRBENE_BENE_LAST_NAME,PZRBENE_BENE_FIRST_NAME,PZRBENE_BENE_MI,PZRBENE_BREL_CODE,PZRBENE_SEX_IND,PZRBENE_BIRTH_DATE,PZRBENE_SSN,PZRBENE_MEMBER_TYPE,PZRBENE_DEL_IND) values (33163,null,null,'Wilson','Janet',null,'E','F',to_date('01-JAN-64','DD-MON-RR'),'888888888','N','N');
Insert into PZRBENE (PZRBENE_PIDM,PZRBENE_SEQ_NO,PZRBENE_BENE_PIDM,PZRBENE_BENE_LAST_NAME,PZRBENE_BENE_FIRST_NAME,PZRBENE_BENE_MI,PZRBENE_BREL_CODE,PZRBENE_SEX_IND,PZRBENE_BIRTH_DATE,PZRBENE_SSN,PZRBENE_MEMBER_TYPE,PZRBENE_DEL_IND) values (33164,null,null,'Choski','Jon',null,'E','M',to_date('14-MAR-52','DD-MON-RR'),'123456789','N','N');
Insert into PZRBENE (PZRBENE_PIDM,PZRBENE_SEQ_NO,PZRBENE_BENE_PIDM,PZRBENE_BENE_LAST_NAME,PZRBENE_BENE_FIRST_NAME,PZRBENE_BENE_MI,PZRBENE_BREL_CODE,PZRBENE_SEX_IND,PZRBENE_BIRTH_DATE,PZRBENE_SSN,PZRBENE_MEMBER_TYPE,PZRBENE_DEL_IND) values (33169,null,null,'Parck','Wilim','G','C','M',to_date('09-MAY-03','DD-MON-RR'),'99999999','D','N');
My Code:
update pzrbene
set pzrbene_seq_no = 1
where pzrbene_brel_code = 'E';
*/
set serveroutput on:
Declare
cursor p_cur is
select pzrbene_seq_no,pzrbene_pidm
from pzrbene
where pzrbene_brel_code <> 'E'
order by pzrbene_pidm;
p_pidm PZRBENE.PZRBENE_PIDM%TYPE;
p_seq_no number := 0;
p_brel_code pzrbene.pzrbene_brel_code%TYPE;
Begin
open p_cur;
fetch p_cur into p_seq_no,p_pidm ;
while p_cur%FOUND
LOOP
update pzrbene
SET PZRBENE_SEQ_NO = p_seq_no + 1 where p_pidm = pzrbene.pzrbene_pidm and p_brel_code= pzrbene_brel_code;
fetch p_cur into p_seq_no,p_pidm;
end loop;
end;
/
答案 0 :(得分:0)
这个PLSQL块对我有用。这里没什么特别的,只是正确的订购问题。 我假设有一个MEMBER_TYPE =&#39; N&#39;是老板。
declare
v_counter number := 0;
cursor cur is select pzrbene_pidm pidm, pzrbene_brel_code brel_code
from pzrbene order by pzrbene_pidm, pzrbene_member_type desc
for update;
begin
for rec in cur loop
if rec.brel_code = 'E' then
v_counter := 1;
else
v_counter := v_counter + 1;
end if;
update pzrbene set pzrbene_seq_no = v_counter where current of cur;
end loop;
end;
执行后输出:
select pzrbene_pidm pidm, pzrbene_seq_no,
pzrbene_bene_last_name||' '||pzrbene_bene_first_name bene_name,
pzrbene_brel_code brel_code, pzrbene_member_type member_type
from pzrbene
order by pzrbene_pidm, pzrbene_member_type desc
PIDM PZRBENE_SEQ_NO BENE_NAME BREL_CODE MEMBER_TYPE
------ -------------- ------------------------------------ --------- -----------
33159 1 Regr Ken E N
33159 2 Reg Den H D
33159 3 Regr Lev C D
33159 4 Reger Ja C D
33161 1 Love Rose E N
33161 2 Lace Hary H D
33163 1 Wilson Janet E N
33163 2 Wilon La C D
33164 1 Choski Jon E N
33169 2 Parck Wilim C D
您可能希望将排序更改为更复杂的内容,例如:
order by pzrbene_pidm,
case when pzrbene_member_type='N' then 1 else 2 end, pzrbene_bene_brel_code
它取决于你,我不知道这些列的含义,这些列更重要,当两行之间的其余部分相等时,它更少。