You want to have an INSTEAD OF INSERT trigger that checks the number of current rows. If it is already 50, you will RAISERROR error using RAISERROR . If not, you just insert write.
A warning! Unconfirmed code ahead. It may contain typos or minor syntax errors. The code is supposed to show you the concepts used. Adjust and adjust your needs accordingly.
Like this:
CREATE TRIGGER checktablelimit ON yourtable INSTEAD OF INSERT AS DECLARE @currentCount INT SELECT @currentCount = COUNT(*) FROM yourtabletolimit IF @currentCount = 50 BEGIN RAISERROR ('Table already has 50 records', 11, 1); END ELSE BEGIN INSERT INTO yourtable (field1, field2, field3) SELECT field1, field2, field3 FROM inserted END GO
source share