School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 309,245 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,671 people online right now. Registration is fast and FREE... Join Now!




SQL query to update serial no in a table

 

SQL query to update serial no in a table

rashmi_nr

11 Dec, 2008 - 02:51 AM
Post #1

New D.I.C Head
*

Joined: 11 Dec, 2008
Posts: 2

I have a table XYZ with fields {Account, Serial No, Beneficiary Name, Status…….so on...}

None of the fields are unique.



Now I wish to generate unique serial numbers to each of the records such that I have:



Account Serial No

A1 1

A2 1

A1 2

A1 3

A3 1

A3 2

A3 3

A3 4



Hence it should dynamically increment serial No for each group of records (with same Account) and reset to 1 for a new group of account.

Once this is done I would be using {Account, Serial No} as the index.



I have the following procedure but it works as below:



Account Serial No

A1 3

A2 1

A1 3

A1 3

A3 4

A3 4

A3 4

A3 4









DECLARE



CURSOR XYZ_CURSOR IS

SELECT account FROM XYZ;



BEGIN --{

FOR XYZ_rec IN XYZ_CURSOR

LOOP --{

BEGIN --{

UPDATE XYZ a

SET a.serial_num = (select to_char(to_number(max(b.serial_num) + 1))

FROM XYZ b WHERE

b.acid = XYZ_rec.acid)

WHERE

a.acid = XYZ_rec.acid;



EXCEPTION

WHEN NO_DATA_FOUND THEN NULL;

END; --}

COMMIT;





END LOOP;--}

COMMIT;

DBMS_OUTPUT.PUT_LINE('ALL Rows Updated :' );



END; --}



Probably its because of the where clause in update ( WHERE a.acid = XYZ_rec.acid; ) which picks all the records with the same account from the table.





Is it possible to do this operation without a unique key to the table?





Please help.

Thanks



User is offlineProfile CardPM
+Quote Post

 
Reply to this topicStart new topic
Replies(1 - 2)

P4L

RE: SQL Query To Update Serial No In A Table

11 Dec, 2008 - 06:37 AM
Post #2

Geek 4 Life
Group Icon

Joined: 7 Feb, 2008
Posts: 2,182



Thanked: 16 times
Dream Kudos: 125
My Contributions
In please help us to help you by placing your code in tags like this code.gif

Thanks

This post has been edited by P4L: 11 Dec, 2008 - 06:37 AM
User is offlineProfile CardPM
+Quote Post

rashmi_nr

RE: SQL Query To Update Serial No In A Table

11 Dec, 2008 - 10:19 AM
Post #3

New D.I.C Head
*

Joined: 11 Dec, 2008
Posts: 2

CODE


DECLARE

CURSOR XYZ_CURSOR IS

SELECT account FROM XYZ;

BEGIN --{

    FOR XYZ_rec IN XYZ_CURSOR

    LOOP --{

    BEGIN --{

        UPDATE XYZ a

        SET a.serial_num = (select to_char(to_number(max(b.serial_num) + 1))

        FROM XYZ b WHERE

        b.acid = XYZ_rec.acid)

        WHERE

        a.acid = XYZ_rec.acid;



        EXCEPTION

        WHEN NO_DATA_FOUND THEN NULL;

    END; --}

    COMMIT;


END LOOP;--}

COMMIT;

DBMS_OUTPUT.PUT_LINE('ALL Rows Updated :' );

END; --}



This post has been edited by rashmi_nr: 11 Dec, 2008 - 10:23 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/26/09 09:28AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month