The crux of my question is the following style of UPDATE statement in a trigger:
update ActualTableName
set X=Y
from inserted
I would have thought that this is a syntax error... the table "ActualTableName" does not appear in the "from" clause.
So... is this just some sort of funky automatic alias "unwind" (i.e. it knows "inserted" is an alias for ActualTableName? This seems unlikely because of a later query (see below).
Or... is there something more... like the query is expanded to:
update ActualTableName
set X=Y
from ActualTableName
cross join inserted
This also gets a bit stranger with a more complicated UPDATE that references both the inserted and deleted tables:
update ActualTableName
set [... some assignments ...]
from
inserted
left outer join deleted
on inserted.Id = deleted.Id
and inserted.SomeField > deleted.SomeField
where
inserted.Id <> ActualTableName.Id and
This query appears to be updating records in ActualTableName that are not part of the inserted table... and this leads me to think that the actual query is:
update ActualTableName
set [... some assignments ...]
from
ActualTableName
cross jon inserted
left outer join deleted
on inserted.Id = deleted.Id
and inserted.SomeField > deleted.SomeField
where
inserted.Id <> ActualTableName.Id and
The books online are a bit opaque about this, and says this:
If the object being updated is the same as the object in the FROM clause and there is only one reference to the object in the FROM clause, an object alias may or may not be specified. If the object being updated appears more than one time in the FROM clause, one, and only one, reference to the object must not specify a table alias. All other references to the object in the FROM clause must include an object alias.
So... can anyone provide clarity as what is going on?