Link to home
Start Free TrialLog in
Avatar of Ihab
Ihab

asked on

How to copy a row and past it in the same table with editing some records in SQL Server Management Studio

How to copy a row and past it in the same table with editing some records in SQL Server Management Studio 2008 , Please if you give me solution in commands give me an example
Thanks
ASKER CERTIFIED SOLUTION
Avatar of Habib Pourfard
Habib Pourfard
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Generally copy/pasting an entire row is not possible, as this would create a duplicate record in the table. This would either result in a primary key violation or, in case there is no primary key (very bad idea!) you'd be stuck with an uneditable record which would probaly require you to truncate the entire table to fix.

You should write an insert statement instead, where in the select portion of the query you apply the changes required. This way the new record is inserted after the required changes have been made.
Since you did not post any sample data or explain exactly what you're trying to copy, we can't provide you with an accurate sample query.
Kvwielink

The management studio is smart enough to allow you copy/paste of a row. it won't create duplicate rows, it recognizes the primary key(identity) and regenerates a new one. also allows to modify the copied row and then saves the changes.
Ok, I haven't really tried it before, but I've gotten stuck on duplicate rows in the past where the management studio did allow me to modify a record to match an existing record. Once that happens, you're pretty screwed.
When you say "regenerates a new one" I guess you're assuming there's an identity column in the table? If I try to copy a single row I'm indeed able to paste it, then edit whatever value I want to change, and then commit it. But if you copy multiple rows, it tries to commit them before they can be edited. If there is no auto PK generation on said table, the records are rejected (just tested it on a test table I have).