Someone had tried to be clever. Looking at the code running, if you’ve been practicing SQL Server for a while, usually means one thing.
A Scalar Valued Function was running!
In this case, here’s what it looked like:
123456789101112 CREATE OR ALTER FUNCTION dbo.BadIdea ( @uid INT )RETURNS BIGINTWITH RETURNS NULL ON NULL INPUT, SCHEMABINDINGASBEGINDECLARE @BCount BIGINT;SELECT @BCount = COUNT_BIG(*)FROM dbo.Badges AS bWHERE b.UserId = @uidGROUP BY b.UserId;RETURN @BCount;END;
Someone had added that function as a computed column to the Users table:
Spoilers: this was a bad idea.