עזרה ב plsql

li2307

New member
עזרה ב plsql

הי, בטבלה קיימת עמודה המכילה קוד לקוח (customerid), כאשר קוד לקוח יכול להופיע פעם אחת או יותר - ועליי להוסיף עמודה לטבלה, ובה לעדכן מספר רץ (החל מ-1) של המופעים של אותו קוד לקוח. כלומר כל פעם שמתחיל בטבלה קוד לקוח חדש עליי להתחיל את המספור (counter) מ-1. דוגמא:
customerid,counter
13579,1
13579,2
13579,3
25877,1
36988,1
36988,2
לא הצלחתי לבצע בתוך cursor עם while. אודה לעזרתכם.
 

ni4ni

New member
תנסה את זה

תנסה לפי הדוגמה הבאה. אני לא סגור אם זה הביצוע הכי יעיל, אבל לי זה עבד:

create table table1 (customerid number(6),name varchar2(50));
Then populate the table with some data.
create table table2 (customerid number(6),name varchar2(50), seq number(6));
Then run this:
declare
cursor main_cur is select distinct customerid from table1 order by customerid;
cursor inner_cur (custid number) is select * from table1 where customerid=custid;
cid number;
all_rec table1%rowtype;
counter number:=0;
begin
for cid in main_cur loop
for all_rec in inner_cur (cid.customerid) loop
counter:=counter+1;
insert into table2 (customerid,name,seq) values (all_rec.customerid, all_rec.name, counter);
end loop;
counter:=0;
end loop;
commit;
end;
/
 
למעלה