Solved

help with update statement

Posted on 2010-09-22
11
446 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
 

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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

747 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

12 Experts available now in Live!

Get 1:1 Help Now