Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Query HELP : Increment by looping

Ricky007May 20 2021

Dear Experts,
11.2.0.4.0
CREATE TABLE ACCT
(
custid VARCHAR2(20),
ACCTID VARCHAR2(3),
flag varchar2(1)
);

INSERT INTO ACCT VALUES('95624','101','P');
INSERT INTO ACCT VALUES('95624',NULL,'N');
insert into acct values('95624',NULL,'N');
INSERT INTO ACCT VALUES('62548','101','P');
INSERT INTO ACCT VALUES('62548',NULL,'N');
INSERT INTO ACCT VALUES('62548',NULL,'N');
Tried Block
BEGIN
FOR I IN
(
SELECT custid ,MAX(ACCTID) AS ACID FROM ACCT
GROUP BY custid
)
LOOP
UPDATE ACCT SET ACCTID=I.ACID+1
WHERE CUSTID=I.CUSTID
AND ACCTID IS NULL;
END LOOP;
END;

Output i am getting is given below
image.png

Here i want to insert next increment starting from 101 for each custid.
Desired result
Note below i had modified on sample table to show desired result.

In my live environment i have around 200 custid i need to generate unique 3 digit number to the respective custid
image.png

Please help.

This post has been answered by cormaco on May 20 2021
Jump to Answer
Comments
Post Details
Added on May 20 2021
2 comments
453 views