Solved

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

Posted on 2011-02-16
2
331 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 500 total points
Comment Utility
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
Comment Utility
Thanks!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now