Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


elegant strategies for complex update statements?


elegant strategies for complex update statements?

Author
Message
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8966 Visits: 19020
sqlguy-736318 (4/4/2013)
What I'm suggesting is that table #a would have 2 rows with the same company. For example:

'Microsoft', 'Seattle'
'Microsoft', 'Portland'

In this scenario, row #1 would get updated but row #2 would not.


That's incorrect, you must be observing something different. Using sample script from above, try this:

create table #a (DomainName varchar(25), OfficeName varchar(25));

create table #b (DomainName varchar(25), OfficeName varchar(25));

insert into #a
values
('microsoft', 'seattle'),
('microsoft', 'Reading'),
('dell', 'huston');

insert into #b
values
('microsoft', 'seattle, wa'),
('dell', 'huston, tx');

select * from #a;

update a
set OfficeName = b.OfficeName
from #a a
inner join #b b on b.DomainName = a.DomainName;

select * from #a;



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
j.miner
j.miner
SSC Journeyman
SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)

Group: General Forum Members
Points: 88 Visits: 358
http://craftydba.com/?attachment_id=5149


Hi SQL Guy,

Unless you try the sample code that I gave you, we really do not know what you are trying to do.

I updated table A to have two records. Table B has one matching record.

After the update, both records in table are updated.

Works like I think it would!

Cheers[url=http://craftydba.com/?attachment_id=5149][/url]

John Miner
Crafty DBA
www.craftydba.com
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search