Solved

I have to create a link between the original row and the updated row when inserting a duplicate row

Posted on 2014-03-15
16
295 Views
Last Modified: 2014-03-17
Hi,

I have to duplicate a row in the database but I have to create a link between the original row and the updated row. The problem is, once the duplicate row is inserted, the id's have been updated and so I need a way of linking the existing record with the new.

Hope this makes sense.
0
Comment
Question by:jazz__man
  • 8
  • 4
  • 3
  • +1
16 Comments
 
LVL 68

Expert Comment

by:Qlemo
ID: 39932394
I reckon the existing ID does not change, though that could be the case from your description. You still have the issue then to know the new ID from the inserted row. Usually you select the max. current ID maybe filtered to get only rows with the "same" values.
0
 
LVL 77

Expert Comment

by:arnold
ID: 39932404
Presumably the new record is the one that needs to reflect the id of the row it updated such that only the original record will have no link reference
In this scenario, you probably have the record data you are updating.
Alternatively, are these duplicate rows the auditing trail that is in a completely different table?
0
 
LVL 2

Author Comment

by:jazz__man
ID: 39933797
Suppose I need to duplicate the following

CustID    FName     SName
1234       Mike         Smith

after the insert it would be
CustID    FName     SName
1235       Mike         Smith

I need to create a temporary link between both existing and new records.
When I do the insert it would be good to have a temp table with

ExistCustID   New CustomerID
1234              1235

By doing this I would be able to link the old to the new, this is what I need to do, but it needs to be done for multiple row insert.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39933816
I think you want to use the OUTPUT clause in the INSERT statment, see here:
http://technet.microsoft.com/en-us/library/ms177564.aspx
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39933932
... if CustID is an autonumber. Using OUTPUT you get the inserted row(s). After that, you will insert the new IDs in your reference table, and then search the original row by using the Fname and SName fields.
0
 
LVL 2

Author Comment

by:jazz__man
ID: 39933942
Guy Hengel

Thanks for this, however I have looked into the output keyword and this only allows columns from the insert statement to be included in the output.

I actually need an id from two different tables to be matched.

Thanks
0
 
LVL 2

Author Comment

by:jazz__man
ID: 39933943
Qlemo

Can you do a basic mockup of how this would work?
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39934025
Are you doing that inside of an application, or with T-SQL?
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 2

Author Comment

by:jazz__man
ID: 39934064
with T-SQL
0
 
LVL 2

Author Comment

by:jazz__man
ID: 39934066
Been messing about with this idea but have not got it to work yet

MERGE T s
USING (
        SELECT TID, name, address
        FROM T [s]
      ) d on 0 = 1
WHEN NOT MATCHED
THEN INSERT (name, address)
    VALUES (name, address)
OUTPUT d.TID as oID, Inserted.TID as nID
INTO @t;
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39934100
> but have not got it to work yet
error message(s) are?
did you declare the @t table?

see this script which is working just fine
use  soap


go

create table tmp_ee ( id int identity , data varchar(10) )
go

insert into tmp_ee (data) values ( 'row 1')
insert into tmp_ee (data) values ( 'row 2')
go

declare @t table ( id1 int, id2 int )

merge tmp_ee t
using (select id, data from tmp_ee ) d
on ( 0 = 1 )
when not matched then insert (data) values (data)
output d.id, inserted.id into @t
;
select * from @t 
select * from tmp_ee


go
drop table tmp_ee

Open in new window

output
(1 row(s) affected)

(1 row(s) affected)

(2 row(s) affected)
id1         id2
----------- -----------
1           3
2           4

(2 row(s) affected)

id          data
----------- ----------
1           row 1
2           row 2
3           row 1
4           row 2

(4 row(s) affected)

Open in new window

as you can see, ID 1 is linked to ID 3 and ID 2 to ID 4

hope this clarifies
0
 
LVL 2

Author Comment

by:jazz__man
ID: 39934133
I don't understand what the s is in MERGE T s

and then again in FROM T [s]
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39934154
that is an alias, for the table, during the query
see here my article about that concept:
http://www.experts-exchange.com/Database/Miscellaneous/A_11135-Why-should-I-use-aliases-in-my-queries.html
0
 
LVL 2

Author Comment

by:jazz__man
ID: 39934187
I understand aliases but is 's' a different alias from '[s]'
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39934206
in this case it's the same. names can be enclosed by [ ]  , which is optional unless you want to use special characters like spaces etc ...

so, the following 4 are fully equivalent
FROM T s
FROM T [s]
FROM [T] s
FROM [T] [s]
0
 
LVL 2

Author Closing Comment

by:jazz__man
ID: 39934340
I haven't actually got it to work yet but I believe this is the way forward.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dimension table indexes 8 13
MS SQL Inner Join - Multiple Join Parameters 2 23
Microsoft Azure SQL - create a read only user 2 16
Sql Join Problem 2 33
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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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 to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

867 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

21 Experts available now in Live!

Get 1:1 Help Now