SQL Combine two rows into one

Check out SQL Procedures. I’m not sure about your goals but I think what you need is to define a SQL Procedure. It’s been I while since I did that, but it should go like:

CREATE OR REPLACE PROCEDURE combine(
           ID1 IN <TABLE_NAME>.ID%TYPE,
           ID2 IN <TABLE_NAME>.ID%TYPE,
           NEWID IN <TABLE_NAME>.ID%TYPE)
    IS
    BEGIN

        INSERT INTO <TABLE_NAME> (ID, Amount) VALUES(NEWID, ((SELECT Amount FROM <TABLE_NAME> WHERE ID=ID1) + (SELECT Amount FROM <TABLE_NAME> WHERE ID=ID2)));
        DELETE FROM <TABLE_NAME> WHERE ID = ID1;
        DELETE FROM <TABLE_NAME> WHERE ID = ID2;

    COMMIT;

END;
/

To get the state as described by you, you can call the routine by

combine(4, 5, 101);

Leave a Comment