Solved

Identity values with multi-row INSERT

Posted on 2004-09-20
3
291 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:Jeff Tennessen
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 8

Author Comment

by:Jeff Tennessen
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

813 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

17 Experts available now in Live!

Get 1:1 Help Now