Solved

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

Posted on 2014-01-08
4
5,932 Views
Last Modified: 2014-01-09
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
0
Comment
Question by:Ihab
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
Habib Pourfard earned 500 total points
ID: 39767307
1. First right click on a table and select "Edit Top 200 Rows"
2. press CTRL + 3
3. Edit query in editor and add condition to find desired records
select * from MyTable
Where [Condition....]
4. press CTRL + R to execute query
5. select a row by clicking on the left side of the row and press CTRL + C to copy the row
6. select last row by clicking on left side of the last record (*)
7. press CTRL + V to paste the record you have copied
8. modify the column values
0
 
LVL 13

Expert Comment

by:Koen Van Wielink
ID: 39767309
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.
0
 
LVL 12

Expert Comment

by:Habib Pourfard
ID: 39767324
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.
0
 
LVL 13

Expert Comment

by:Koen Van Wielink
ID: 39767352
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).
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
TSQL Challenge... 7 52
monitoring configuration for SQL server DB 32 60
Index and Stats Management-Specific tables 8 49
Question about Common Table Expressions 3 39
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question