Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 472
  • Last Modified:

help with update statement

Hello, I have a table that contains two types of users: customers and partners

Customers choose partners from a list when creating an account.

I had to recreate all the accounts which gave new accountId to all accounts. I now need to update the customer's partnerId with the new accountId of their chosen partner. Make sense?

These are the fields that matter: accountId, partnerid, oldaccountId

I thougt that if I put all the new accountid and oldaccoutid in a table I could use that as a join table, but that is not updating the old partnerId with the new accountid. below is my update statement that is not working.
create table #Temp(
nid uniqueidentifier,
oid uniqueidentifier)
Insert #Temp
select accountId, oldaccountid from accounts

begin tran
update accounts
set partnerId = b.nid
from #Temp b, accounts a
where a.partnerid = b.oid

Open in new window

0
gogetsome
Asked:
gogetsome
  • 3
  • 3
  • 3
  • +1
1 Solution
 
cyberkiwiCommented:
update a
set partnerid = p.accountid
from accounts a
inner join accounts p on p.oldaccountId = a.partnerId
0
 
anyoneisCommented:
I like to first do a select that show me what is going to be modified. Then, I just highlight the actual update, skipping the select and the "--", as in:
select a.accountid, b.nid
-- begin transaction; update a set a.partnerid = b.nid
from accounts a
join #Temp b on a.partnerid =  b.oid
 
So, run the select and ensure that the partner id is beinbg paired with the partner's new id. Then select the "begin transaction" on down  and run it.
Verify your results then commit.
 
David
0
 
tabishCommented:
I do not see any issue with the update query itself in terms of logic. As long as you know what the data is like.  

As anyoneis suggested first run the select statement to make sure your query retun the correct results.

Secondly, not sure if you are commiting your transaction as I cannot see it in the code provided. Make sure you do it.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
gogetsomeAuthor Commented:
Well, I have tried all of the queries again and still not working.

Before I commit the transaction I grab a partnerid do a select on the table with that value where it equals an accountId and the result set is null. It should bring back the partner account that the user has selected.
0
 
cyberkiwiCommented:
update accounts
set partnerId = b.nid
from #Temp b, accounts a
where a.partnerid = b.oid

This statement can have indeterminate results.
In your FROM clause, you have aliased Accounts, but you are updating "another" table "Accounts" with no link to either a or b.

As shown in first comment, a temp table is unnecessary.
0
 
gogetsomeAuthor Commented:
Thank you cyberkiwi. I thied

update a
set partnerid = p.accountid
from accounts a
inner join accounts p on p.oldaccountId = a.partnerId

and when I grab a partnerId and query accountid for a match there is none; nor is there one when I query oldaccountid.
0
 
cyberkiwiCommented:
Without using a temp table - this query is designed to be run only once. If you run it a few times... you may get very very strange data.

Start with just one account.

select top(1) accountid, oldaccountid, partnerid from accounts
where partnerid is not null

Using the partnerid from above, run this:

select accountid, oldaccountid
from accounts
where accountid = <the partnerid>

Using the partnerid from first query, run this:

--update a
--set partnerid = p.accountid
select a.accountid, a.oldaccountid, a.partnerid old, p.accountid newpartner
from accounts a
inner join accounts p on p.oldaccountId = a.partnerId
where a.accountid = <the accountid>

Does everything check out?
0
 
anyoneisCommented:
update new
set new.partnerid = old.accountid
from accounts new
join acounts old on new.partnerid = old.oldaccountid
Pretty much the same as cyberkiwi, but the target in the set is aliased - maybe that makes a difference.
0
 
tabishCommented:
"Before I commit the transaction I grab a partnerid do a select on the table with that value where it equals an accountId and the result set is null. It should bring back the partner account that the user has selected."

If the Transaction is open you will not see the results unless you commit it or rollback. Can you please run select statement before begining the transaction and see if the query retuns the correct results?

0
 
anyoneisCommented:
I am in the habit of doing a verification select before I do the commit - you should be able to do that since you are in the same session as the one locking the rows.
0
 
gogetsomeAuthor Commented:
Ok, this is does work! Thank you!!

My problem was about 16k of orphan partnerId that did not have an associated OldAccountId.

I have a mess on my hands and will have to look into getting the proper info from backups prior to me picking up this job. Looks like the last developer lost a bunch of partner accounts or did not handle for updating the table when deleting a partner!

BTW, Yes you can query before commit as long as the session is the same. What good would commit/rollback be if you could not. I advise in checking the result before commiting!!!!

Awesome help guys!!
ARGH!!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now