SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Mapping Old identity values to new ones


Mapping Old identity values to new ones

Author
Message
chandrakant_gaurav
chandrakant_gaurav
SSC Rookie
SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)

Group: General Forum Members
Points: 43 Visits: 174
Hi All,

I have a requirement in which I insert values into same table, but I need to map old identity column with those newly inserted: Below is the snippet:
1st Insert:
INSERT INTO [dbo].[tblOrders] ...........--some insert statement
SET @newOrderId = @@IDENTITY;

CREATE TABLE #temp_result (OldOrderItemID BIGINT, NewOrderItemID BIGINT)

INSERT INTO [dbo].[tblOrderItems]
([ItemTypeID]
,[OrderID]
,[Service]
)
SELECT
[ItemTypeID]
,@newOrderId
,[Service]
FROM [dbo].[tblOrderItems] A
WHERE A.OrderID = @OrderID -- Multiple records for single @orderID

In table tblOrderItems there is Identity: OrderItemID Seed 1 Increment 1

I need a table #temp_result with entry for mapping old OrderItemIDs with newly inserted OrderItemIDs without disturbing the existing tblOrderItems. Any advice?
Kingston Dhasian
Kingston Dhasian
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3685 Visits: 5175
Is there a UNIQUE column(s) in your table tblOrderItems other than the IDENTITY field?


Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Kingston Dhasian
Kingston Dhasian
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3685 Visits: 5175
Assuming that the column combination of ItemTypeID, Service and OrderID is UNIQUE, the below code should do what you need

INSERT   [dbo].[tblOrderItems]( [ItemTypeID], [OrderID], [Service] )
SELECT [ItemTypeID], @newOrderId, [Service]
FROM [dbo].[tblOrderItems] A
WHERE A.OrderID = @OrderID

INSERT #temp_result( OldOrderItemID )
SELECT A.OrderItemID
FROM [dbo].[tblOrderItems] A
WHERE A.OrderID = @OrderID

UPDATE R
SET R.NewOrderItemID = NOI.OrderItemID
FROM #temp_result AS R
INNER JOIN [dbo].[tblOrderItems] AS OOI
ON R.OldOrderItemID = OI.OldOrderItemID
INNER JOIN [dbo].[tblOrderItems] AS NOI
ON OOI.ItemTypeID = NOI.ItemTypeID
AND OOI.Service = NOI.Service
AND NOI.OrderID = @newOrderId
WHERE R.NewOrderItemID IS NULL




Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search