Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 merging account together

Author  Topic 

ntn104
Posting Yak Master

175 Posts

Posted - 2009-07-07 : 09:04:09
Hello,

How do I merge one account to another account. Please see below for data:

Account 1:
ID=1234
Account 2:
ID=5678

I want to merge account 1 into account 2 and delete account 1. I wonder if I can do like this. Thanks,

update t2 set
id=1234
from table as t2
whereid =5678
and delete id=1234

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2009-07-07 : 09:07:44
Depends what constraints you have on the table and whether you have any foreign keys. If you have reference data in other tables, you will need to look into getting that sorted first. Once you have done this, you can quite easily just delete this record if you already have all of the rest of the information on your current 1234 account.
Go to Top of Page

ntn104
Posting Yak Master

175 Posts

Posted - 2009-07-07 : 09:12:22
quote:
Originally posted by RickD

Depends what constraints you have on the table and whether you have any foreign keys. If you have reference data in other tables, you will need to look into getting that sorted first. Once you have done this, you can quite easily just delete this record if you already have all of the rest of the information on your current 1234 account.



But there some reference data under account id=1234 that need to be merge to account id=5678. If I delete 1234, then some information like case_id under 1234 will be delete and can not merge with id=5678.
Go to Top of Page

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2009-07-07 : 09:46:00
So you'll have to update the reference data first, as I said. Once this is done, you can then delete the other record. I am guessing that accountid is your primary key, so having duplicates is not good practice and if you have set it to be a primary key, you will have a constraint violation.

Update the record you want to keep with the information from the other record first and then delete the other one.

Check any other tables for reference data before you delete the record.
Go to Top of Page

ntn104
Posting Yak Master

175 Posts

Posted - 2009-07-07 : 09:51:59
quote:
Originally posted by RickD

So you'll have to update the reference data first, as I said. Once this is done, you can then delete the other record. I am guessing that accountid is your primary key, so having duplicates is not good practice and if you have set it to be a primary key, you will have a constraint violation.

Update the record you want to keep with the information from the other record first and then delete the other one.

Check any other tables for reference data before you delete the record.



Thank you! I guess I have to update reference data first as you said.
Go to Top of Page
   

- Advertisement -