Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL to copy a record and insert it using a specified new primary key

Posted on 2011-02-16
2
Medium Priority
?
340 Views
Last Modified: 2012-06-22
I have an 80 coluimn table with lots of records and the Primary key is a column named OrderNum (and is NOT an identity field). I need to insert a new record with all identitical values as an existing record based on the OrderNum, except obviously the OrderNum can't be the same, so I need to assign the newly inserted OrderNum.

What would be the best SQL statement to accomplish this?
Let's say the record I need to copy is OrderNum=100500
and I need to assign the new record Ordernum=101880
0
Comment
Question by:bobPUNKbob
2 Comments
 
LVL 26

Accepted Solution

by:
tigin44 earned 2000 total points
ID: 34911368
try a syntax like this

INSERT INTO yourTable (OrderNum, col1, col2,....)
SELECT 101880, col1, col2,....
FROM yourTable
WHERE OrderNum = 100500
0
 

Author Closing Comment

by:bobPUNKbob
ID: 34912168
Thanks!
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

782 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