tags:

views:

73

answers:

2

I need to duplicate one row changing the PK. The table can be different in each client installation, so I can't just enumerate the columns. I've managed to do the following:

INSERT INTO table SELECT * FROM table WHERE PK='value'

but obviously it fails because I'm trying to duplicate the PK.

Then I tried:

INSER INTO table SELECT 'newValue' AS PK, * FROM table WHERE PK='value'

It also failed, because the column names didn't match.

I know the PK will always be the first column, but I'm not sure it's of much use.

So... Is this possible? Any idea?

+3  A: 

The only solution is to build the query dynamically by querying for its list of columns and excluding identity column (which is why I'm assuming you wish to skip the PK).

Thomas
Of course, PK is short for Primary Key.
Jaime Pardos
As there's no better answers, I'm accepting this, which seems to be correct.
Jaime Pardos
+1  A: 
INSERT INTO table1 (col1_PK, col2 col3) 
SELECT newValue, col2, col3
FROM table1 
WHERE col1_PK = 'Value'
As the OP said, he doesn't know the column names beforehand.
Ian Henry