Solved

help with update statement

Posted on 2010-09-22
11
454 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

786 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