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,427 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
  • 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Writing SQL Select Query result to a text file 12 46
SQL Server Question 5 36
sql help 8 55
ms sql + get number in list out of total 7 29
I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

773 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