Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Update with cross apply
Message
De
28/11/2013 16:15:19
 
 
À
Tous
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Titre:
Update with cross apply
Versions des environnements
SQL Server:
SQL Server 2008
Application:
Web
Divers
Thread ID:
01588861
Message ID:
01588861
Vues:
43
I would like to verify an update syntax with a cross apply.

I have a Member table which contains one or more messages in the MemberMessage table.

In the Member table, I have created a field Member.MessageAddDate which represents the MemberMessage.AddDate of the first child record created.

Now, I need to execute a SQL command that will fill that field:
UPDATE Member SET MessageAddDate=MemberMessage.AddDate
 FROM Member
 CROSS APPLY (SELECT TOP 1 MemberMessage.AddDate FROM MemberMessage
  WHERE Member.Numero=MemberMessage.NoMember ORDER BY MemberMessage.Numero) MemberMessage
This seems to be ok. I see I get 11846 records updated.

Now, I need to do the same thing with another field named Member.MessageModDate which would represent the most recent child record created:
UPDATE Member SET MessageModDate=MemberMessage.AddDate
 FROM Member
 CROSS APPLY (SELECT TOP 1 MemberMessage.AddDate FROM MemberMessage
  WHERE Member.Numero=MemberMessage.NoMember ORDER BY MemberMessage.Numero DESC) MemberMessage
As you can see, I only changed the Member replace field and adjusted the CROSS APPLY to get the most recent record.

I know in the child table I have more than one occurence per master record. However, when I execute the second command, it only replaces the same values as the first command. I would have expected this one to grab the most recent MemberMessage.AddDate for some of them, as some of them contains more than one child record, thus the MemberMessage.AddDate would be more recent.

Anyone knows what I am missing?
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform