Solved

help with update statement

Posted on 2010-09-22
11
458 Views
Last Modified: 2012-05-10
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
Comment
Question by:gogetsome
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33740539
update a
set partnerid = p.accountid
from accounts a
inner join accounts p on p.oldaccountId = a.partnerId
0
 
LVL 11

Expert Comment

by:anyoneis
ID: 33740574
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
 
LVL 4

Expert Comment

by:tabish
ID: 33740685
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
Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

 

Author Comment

by:gogetsome
ID: 33740748
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
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33740800
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
 

Author Comment

by:gogetsome
ID: 33740827
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
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 33740864
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
 
LVL 11

Expert Comment

by:anyoneis
ID: 33740882
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
 
LVL 4

Expert Comment

by:tabish
ID: 33740887
"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
 
LVL 11

Expert Comment

by:anyoneis
ID: 33740893
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
 

Author Closing Comment

by:gogetsome
ID: 33744886
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql query help 15 55
SQL Add Parameter in Variable 4 22
Need help separating values from a column and creating a new record 6 47
Change this SQL to get all nodes 3 40
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

726 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