Solved

Identity values with multi-row INSERT

Posted on 2004-09-20
3
288 Views
Last Modified: 2008-03-06
Hello all,

I have a feeling the answer to this question is going to be a resounding "no," but I 'm wondering if there's a way to accomplish this without using a cursor: I have a table that has no valid natural key, so I use an identity column as a surrogate key. At times, certain rows in this table need to be duplicated. In other words, a group of rows will be selected based on given criteria, and all columns from those rows (except, obviously, the identity) will be inserted into new rows in the same table (with, obviously, new identities). I have to be able to link each newly created row back to the row it was created from in order to create an audit trail of which rows were duplicated into which other rows. I may not be explaining this well; let me know if it seems unclear.

So far, the only way I have found to accomplish this is to use a cursor to loop through the records to be duplicated, inserting each new record, getting its identity using SCOPE_IDENTITY(), then creating the audit trail record at that point. This works OK most of the time, but it seems inefficient to me. Does anyone have a better suggestion?

Thanks in advance!

Jeff
0
Comment
Question by:JTennessen
3 Comments
 
LVL 7

Expert Comment

by:natloz
ID: 12105779
That sounds like the way I would do it...you need to get the Identity as you insert so that you can write it back to the other record...cursor sounds like the best way to do it...
0
 
LVL 15

Accepted Solution

by:
jdlambert1 earned 250 total points
ID: 12105793
Yes, you can. Here's a simple example. If your table has only two columns, ID (the identity) and ColA, then you add a third column, named "ID_Source". Then you do an insert like this:

INSERT TableA(ID_Source, ColA)
SELECT ID, ColA FROM TableA WHERE ...
0
 
LVL 7

Author Comment

by:JTennessen
ID: 12106197
Thanks, jdlambert1. Though it requires a schema modification, I think it might be worth it in this case. Much appreciated!

Jeff
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

758 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

19 Experts available now in Live!

Get 1:1 Help Now