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
Microsoft SQL Server 2008

Avatar of undefined
Last Comment
Koen Van Wielink

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Habib Pourfard

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Koen Van Wielink

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.
Habib Pourfard

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.
Koen Van Wielink

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).
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck