Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table structured like this
EMP(name varchar(45), sal money...);

i want the users to enter up to 20 records only..if 21 st record is entered an exception has to be raised... i want to do this witout the use of triggers..

Please help me
Posted

There are two places that I see that this could be done. First, you could create a stored procedure that would take up to twenty records as an input. The stored procedure could throw an error if more than that were passed in (at once). The downside here is that you could call the stored procedure more than once. However, you could get complicated and have the stored procedure look up the table and ensure that the user had not already entered 20 values before the insertion began.

The other option would be to put this logic into your application. Have the application do a lookup on the table to see how many records the user had already entered. Then, only allow the remaining number of records to be added.

No matter what you do, it won't be as foolproof as a trigger, but it is possible to achieve your requirements using either of the two methods listed above.
 
Share this answer
 
Comments
shashavali 29-Jun-12 9:21am    
thank you
Hi,
Use this:
SQL
DECLARE @count INT
SET @count = (SELECT COUNT(*) FROM test1)
IF @count > 20
BEGIN
   PRINT 'You cannot insert more than 20 records'
END
ELSE
   INSERT INTO test1 VALUES('1', 'Amit', 'Bangalre')

All the best.
--Amit
 
Share this answer
 
Comments
shashavali 29-Jun-12 9:21am    
thank you
_Amy 29-Jun-12 9:26am    
Welcome shashavali. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900