3

I want to set the limit for my table's rows. How can I do it?

For example 50 rows in my table.

Ray
  • 45,695
  • 27
  • 126
  • 169
ahmadali shafiee
  • 4,350
  • 12
  • 56
  • 91

4 Answers4

13

Create an AFTER INSERT trigger on the table. Here's something that would be relatively effective with your requirement:

create trigger LimitTable
on YourTableToLimit
after insert
as
    declare @tableCount int
    select @tableCount = Count(*)
    from YourTableToLimit

    if @tableCount > 50
    begin
        rollback
    end
go
  • -1 That assumes the application does not have a transaction control itself (Autocommit). This would break the app's if the `INSERT` is, say, the 3rd data change query in a chain of, say, 5 queries. – Adriano Carneiro Oct 28 '11 at 14:17
  • This can fail under snapshot isolation. – Martin Smith Oct 28 '11 at 14:18
  • 1
    @MartinSmith I understand that, but given the information the OP provided, this is the solution I came up with. There are obviously a few "gotchas" with certain configuration, but without knowing if they exist this is my best guess for a solution. –  Oct 28 '11 at 14:23
  • this doesn't work for idea to have max 50, so 51th row would unshift the first value but would be added – luky Jul 18 '18 at 13:01
6

Use a CHECK constraint. E.g.:

CREATE TABLE t1 (x TINYINT NOT NULL UNIQUE CHECK (x BETWEEN 1 AND 50));
nvogel
  • 24,981
  • 1
  • 44
  • 82
  • Interesting approach, I like your "thinking outside the box". However, who would be responsible for giving `x` a value? Only this will determine if your approach will work. – Adriano Carneiro Oct 28 '11 at 14:37
  • I'm manging a queue so maybe it can be an IDENTITY and reseed when cleaning, ritgh? Thanks – GBrian Dec 16 '16 at 12:25
2

Are you referring to limiting the results of a query?

If so, with SQL Server 2008 you can use TOP

SELECT TOP 50 *
FROM Table

If you're looking at actually limiting the amount of records in the database table, then an IF statement in a TRIGGER, like @Shark has posted, would be my solution.

Community
  • 1
  • 1
Curtis
  • 101,612
  • 66
  • 270
  • 352
2

What you want is having a INSTEAD OF INSERT trigger that checks the # of current rows. If already 50, you will raise an error by using RAISERROR. If not, you just insert the record.

Warning! Untested code ahead. It might contain typos or slight syntax errors. The code is supposed to show you the concepts involved. Tweak and adjust to 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 
Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
  • 1
    This won't be reliable if more than one row is inserted in a single INSERT statement. – nvogel Oct 28 '11 at 14:36
  • Also it won't handle concurrent inserts. 2 transactions could both do `SELECT COUNT(*) FROM yourtabletolimit` and read that value and determine that their insert won't breach the limit. – Martin Smith Oct 28 '11 at 14:40
  • @dportas You are also right. Damn it, apparently SQL Server does not have a `BEFORE INSERT` trigger. This is so easily handled by Oracle, Firebird, PostgreSQL... Of course, they do not solve the point raised by @MartinSmith. I'll leave my answer here, maybe someone can see further on the top of it. – Adriano Carneiro Oct 28 '11 at 14:44
  • @Adrian - Shame there are so many restrictions on indexed views as well. I was trying to think of a way of using one of those with a unique constraint but foiled at every attempt! – Martin Smith Oct 28 '11 at 14:59
  • @MartinSmith I liked dportas' approach (unique constraint), I just don't see how to make it work as well... – Adriano Carneiro Oct 28 '11 at 15:33