Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Update with cross apply
Message
From
28/11/2013 16:15:19
 
 
To
All
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Title:
Update with cross apply
Environment versions
SQL Server:
SQL Server 2008
Application:
Web
Miscellaneous
Thread ID:
01588861
Message ID:
01588861
Views:
44
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
Next
Reply
Map
View

Click here to load this message in the networking platform