You can use AFTERtrigger UPDATE/INSERT:
SQLFiddleDEMO
CREATE TABLE tab(col NVARCHAR(100));
CREATE TRIGGER trg_tab
ON tab
AFTER INSERT, UPDATE
AS
IF (SELECT COUNT(*) FROM tab WHERE col = 'username') > 10
ROLLBACK;
INSERT INTO tab(col)
VALUES ('username'), ('username'), ('username'), ('username'),
('username'), ('username'), ('username'), ('username'),
('username'),('username');
INSERT INTO tab(col)
VALUES ('username');
Or use the restriction SCALAR FUNCTIONand CHECK:
SQLFiddleDemo2
CREATE TABLE tab(col NVARCHAR(100));
CREATE FUNCTION dbo.CheckFunction()
RETURNS INT
AS
BEGIN
RETURN (SELECT COUNT(*) FROM tab WHERE col = 'username')
END;
ALTER TABLE tab
ADD CONSTRAINT chkRowCount CHECK (dbo.CheckFunction() <= 10 );
INSERT INTO tab(col)
VALUES ('username'), ('username'), ('username'), ('username'),
('username'), ('username'), ('username'), ('username'),
('username'),('username');
INSERT INTO tab(col)
VALUES ('username');
source
share