SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Complicated (for me) update
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

WJHamel
Aged Yak Warrior

USA
614 Posts

Posted - 03/12/2012 :  10:25:39  Show Profile  Reply with Quote
Trying to get my head around this:

I have two tables, MNInoDUP, and Arrest_DCSO.
Both have identical data, except MNInoDup contains one entry per person, based on Fname, Mname, Lname, Race, Height, Sex. Each person in Mninodup has a unique MNINO indentifying them. Arrest_DCSO has multiple entries per person, but, for the same person, there are different MNINO's. This is the error which needs to be fixed. So, what i need to do is update the arrest_DCSO table so that the MNINO for each person, matches the MNINO that exists for that same person in the MNInoDup table. In arrest_DCSO, the same person, with the same MNINO, Fname, Mname, Lname, Race, Height, Sex can occur multiple times. in the MNINODUP table, each can and does, occur only once.

Where do i start?

thanks

James

webfred
Flowing Fount of Yak Knowledge

Germany
8513 Posts

Posted - 03/12/2012 :  10:30:39  Show Profile  Visit webfred's Homepage  Reply with Quote
Where do i start?

To make it easy for us you can start with
table structure
sample data
wanted result in relation to sample data



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 03/12/2012 :  10:31:30  Show Profile  Reply with Quote

UPDATE a
SET a.MNINO = m.MNINO 
FROM MNInoDUP m
INNER JOIN Arrest_DCSO a
ON a.Fname = m.Fname
AND COALESCE(a.Mname,'') = COALESCE(m.Mname,'')
AND a.Lname = m.Lname
AND COALESCE(a.Race,'') = COALESCE(m.Race,'')
AND COALESCE(a.Height,'') = COALESCE(m.Height,'')
AND COALESCE(a.Sex,'') = COALESCE(m.Sex,'')
WHERE a.MNINO <> m.MNINO 


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

WJHamel
Aged Yak Warrior

USA
614 Posts

Posted - 03/12/2012 :  10:58:10  Show Profile  Reply with Quote
Thanks visakh. Worked perfectly.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 03/12/2012 :  11:28:53  Show Profile  Reply with Quote
welcome
I hope you want the duplicates in Arrest_DCSO with same MNINO to be retained else you need to put a delete logic for them to be eliminated

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

WJHamel
Aged Yak Warrior

USA
614 Posts

Posted - 03/12/2012 :  11:53:36  Show Profile  Reply with Quote
retaining them in there was the idea, yes. thanks again.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 03/12/2012 :  12:31:54  Show Profile  Reply with Quote
ok...then its fine

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

WJHamel
Aged Yak Warrior

USA
614 Posts

Posted - 03/14/2012 :  11:26:57  Show Profile  Reply with Quote
I meant to ask, why use coalesce in this statement?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 03/14/2012 :  12:45:10  Show Profile  Reply with Quote
to make sure you can even taking cases where those fields are having NULL values. All operators like >,<,<> etc ignore NULL values as its not internally stored as a value. so in such cases idea is to convert them to default value using COALESCE and then compare

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

WJHamel
Aged Yak Warrior

USA
614 Posts

Posted - 03/14/2012 :  20:03:14  Show Profile  Reply with Quote
That's what i figured. My boss asked why they were being used and the issue of NULLS was the same answer i gave him. So that being the case, i'm unsure about why he woudn't use them in this case. But then again, everyone around here uses NOLOCK in EVERY script, even though i keep getting cautioned against it by others outside of our circles.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 03/15/2012 :  15:38:32  Show Profile  Reply with Quote
quote:
Originally posted by WJHamel

That's what i figured. My boss asked why they were being used and the issue of NULLS was the same answer i gave him. So that being the case, i'm unsure about why he woudn't use them in this case. But then again, everyone around here uses NOLOCK in EVERY script, even though i keep getting cautioned against it by others outside of our circles.


see whats the issue of using NOLOCK especially in OLTP tables

http://visakhm.blogspot.com/2010/02/avoiding-deadlocks-using-new.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 03/15/2012 :  15:44:44  Show Profile  Reply with Quote
WJ...I hope you are taking backups defore all of these updates

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

WJHamel
Aged Yak Warrior

USA
614 Posts

Posted - 03/15/2012 :  16:19:29  Show Profile  Reply with Quote
indeed. i live redundantly.

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 03/15/2012 :  16:21:15  Show Profile  Reply with Quote
quote:
Originally posted by WJHamel

indeed. i live redundantly.




consider using snapshot isolation level over NOLOCK

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000