Search code examples
sqldb2ibm-midrangerpg

Need SQL command that will insert a row while taking id from row


I need SQL command that will insert a row after specific row. Example:-

Before table

Id.         Name.      
1.           Xyz.          
2.           Xyz
3.           Xyz    

Want result need to add 'Abc' data after each 'xyz' having same id like:-

Id.         Name.      
1.           Xyz.    
2.           Xyz
3.           Xyz
1.           Abc
2.           Abc
3.           Abc

Note this command work on 1000 data


Solution

  • Try using an INSERT INTO ... SELECT:

    INSERT INTO yourTable (id, name)
    SELECT id, 'Abc'
    FROM yourTable
    WHERE name = 'Xyz';
    

    This assumes that you only want to duplicate rows having Xyz as the name. If you instead want to duplicate every record with an Abc version, then just remove the WHERE clause.