Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 INSERTING A RECORD FROM THE SAME TABLE WITH CONSTRANTS

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-03-04 : 22:21:10
Finbar writes "I want to insert a record from the same table with one field value that is different. Similar to the example below.

INSERT INTO tabelename (the table you want to copy into)
SELECT * from tablename (the table you want to copy from)
WHERE Field10 = (some condition)

This would work fine but because of the contraint I get the
Violation of UNIQUE KEY constraint error.

How do I get around this?"

chadmat
The Chadinator

1974 Posts

Posted - 2002-03-04 : 22:25:19
INSERT INTO tabelename (the table you want to copy into)
SELECT col1, col2, <the new value> from tablename
WHERE Field10 = (some condition)

-Chad

Go to Top of Page

dsdeming

479 Posts

Posted - 2002-03-05 : 13:49:26
Just make sure that the select portion of your insert statement doesn't include a reference to any column with a primary key or unique constraint in the table.

For instance if column aa is a PK column, you can't say:

INSERT INTO tablename( aa, ab, ac )
SELECT aa, ab, 'bar' FROM tablename WHERE ac = 'foo'

This will violate the PK constraint.


Go to Top of Page
   

- Advertisement -