Here are 4 examples of writing an UPDATE .. FROM ..I was wondering if anyone has any comments on which syntax is best ?Which is more readable ?, more clear ?These go against the pubs database and all give the same execution plan.Also the condition: s.ord_date = (SELECT MAX(sales.ord_date) FROM sales)
You can put in the WHERE section, but you sould also put it in the JOIN ON section.In my eyes this would go in the WHERE section./* 0. This is the BOL example, ANSI JOIN ? */UPDATE titles SET ytd_sales = titles.ytd_sales + sales.qty FROM titles, sales WHERE titles.title_id = sales.title_id AND sales.ord_date = (SELECT MAX(sales.ord_date) FROM sales)/* 1. Not specifying titles in FROM clause */UPDATE titles SET titles.ytd_sales = titles.ytd_sales + s.qtyFROM sales sWHERE titles.title_id = s.title_id AND s.ord_date = (SELECT MAX(sales.ord_date) FROM sales)/* 2. Specifying titles in from clause, optionally aliasing it */UPDATE titles SET titles.ytd_sales = t.ytd_sales + s.qtyFROM titles t JOIN sales s ON t.title_id = s.title_idWHERE s.ord_date = (SELECT MAX(sales.ord_date) FROM sales)/* 3. Specifying alias in the UPDATE clause */UPDATE t SET t.ytd_sales = t.ytd_sales + s.qtyFROM titles t JOIN sales s ON t.title_id = s.title_idWHERE s.ord_date = (SELECT MAX(sales.ord_date) FROM sales)
/rockmoose/* Chaos is the nature of things...Order is a lesser state of chaos */