Get the Most Current Row From a Detail Table


Task: Update a table with the most current value of a detail table.

In this article I’m using the sys.tables / sys.columns tables because they are available everywhere. In reality it would be, e.g. a customer table, where you are setting the last_order_date on the product table where you want the latest price.

For ideal performance the detail table should have an index on the main ID (object_id in the example or the customer_id in a more realistic scenario) plus the detail id (column_id / order_id) and includes the attribute (column name / order_date), too. This column is part of