I recently encountered the problem that COUNT(*)
requires the user to have select permission on every single column of a table.
Even though the spec of 'COUNT(*)' explicitly says that
it does not use information about any particular column.
It just returns the number of rows in the result.
So if you want to count the number of of rows in a table as a restricted user you get permission exceptions.
Here is an example:
CREATE TABLE [Product]
([name] nvarchar(100) null, [price] float)
CREATE USER Intern WITHOUT LOGIN;
DENY SELECT ON [Product] (price) TO Intern;
EXECUTE AS Intern;
-- Fails with "The SELECT permission was denied on the column 'price' of the object 'Product'"
SELECT COUNT(*) FROM [Product];
REVERT;
After some testing I found that even
SELECT COUNT(1) FROM [Product]
does not work.
Can someone explain what the reasoning behind this behaviour is?
And what would be a workaround to allow the Intern
user to still get an accurate count of Product
.
Update: I would be most interested in workarounds that the Intern could use. So even though creating a View would be best practice for the admin, the Intern does not have this option.