Click here to Skip to main content
15,923,197 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hey guys...I have created a simple table in SQL server 2005...and i want that only 5 records should be inserted in my table.

I am providing you the code below (if you want):-

create table Friends
(
First_Name varchar (50),
Last_Name varchar (50),
Address varchar (50),
ContactNo bigint,
EMailID varchar (20),
RegistrationDate datetime
)


Please provide me the code or query to restrict the no. of records to be inserted in my table "Friends" in SQL.

Thanx
Posted

declare @rowCount as int                 
select @rowCount= COUNT(*) from Friends
print @rowCount --to see the number of records.
if @rowCount<5
insert query
 
Share this answer
 
v3
In order to restrict number of records with Insert, Update or Delete statements use TOP keyword.
Here is the example:

SQL
create table test (id int)

insert top (3) into test (ID) values(1),(2),(3),(4),(5)

select * from test


Output will be:
1
2
3
 
Share this answer
 

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