Data Tales 10: It's a matter of existence
This is the tenth tale in a series of stories about data. I hope you enjoy the series.
I regularly see code that calculates a count of a set of rows then makes a decision that’s based on the count, but only on whether or not the count is zero. Consider this (slightly contrived) example based on AdventureWorks:
DECLARE @PersonCount int;
SELECT @PersonCount = COUNT(*)
FROM Person.Password
WHERE BusinessEntityID = 12
AND PasswordHash = 0x208394209302;
IF @PersonCount > 0
BEGIN
PRINT N'Password matches';
END;
Now I’ll start by saying that the last thing I’d ever want to see in SQL Server code is this sort of playing around with users and passwords, but I’m ignoring that for now.
2025-10-09