views:

56

answers:

2

Please note: I am asking the question I want answered. I know this question means the database is set up poorly. So I will vote down any questions that suggest changing the way the table is set up.

I need to duplicate a bunch of rows, while changing one value.

name   col1 col2
dave   a    nil
sue    b    nil
sam    c    5

needs to become:

name   col1 col2
dave   a    nil
dave   a    a
sue    b    nil
sue    b    a
same   c    5

IE for all entries in this table where col2 is null, create a new entry in the table where name and col1 are the copied, and col2 is a.

+1  A: 

Use:

INSERT INTO table
  (name, col1, col2)
SELECT t.name, t.col1, 'a'
  FROM TABLE t
 WHERE t.col2 IS NULL

That's assuming neither the name or col1 columns are a primary key or have a unique constraint on either.

OMG Ponies
Previous comment deleted: I found my bug. Yep, this worked. Thanks!
David Oneill
+1  A: 

Will this do it?

INSERT INTO yourtable
       (SELECT name, col1, 'a'
          FROM yourtable 
         WHERE col2 is NULL);
DCookie
+1 good answer, although it doesn't actually work in my case. I'm only populating some of the fields in the table.
David Oneill
Should, but risks an ORA error if there's more columns in the table.
OMG Ponies
Yup, the assumption was the table had the columns in the example.
DCookie