?
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
Medium Priority
?
6,449 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

801 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