I'm wondering about the following query :
UPDATE statisticsTable
SET Value = (select count(*)
FROM OtherTable o
WHERE o.UserId = UserId ) <-- this is the part that concerns me
WHERE id in (1,2,3)
How does SQL Server know that the second "UserId" field comes from statisticsTable and not from OtherTable ?
Why can't I give an alias to statisticstable like 'stat' to clarify where I want to get that UserId ? Or is there a way?
WHERE o.UserId = statisticsTable.UserIdto be sure.UPDATE statisticsTable as sand then haveWHERE o.UserID = s.UserIdin your subquery? I haven't tested this though. I've never had an UPDATE where a subquery has been neccessary.