限制号码SQL中的记录数 [英] Restrict the no. of records in SQL

查看:58
本文介绍了限制号码SQL中的记录数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好...我在SQL Server 2005中创建了一个简单的表...我希望只将5条记录插入到我的表中.

我正在为您提供以下代码(如果您愿意):-

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
)



请向我提供代码或查询以限制编号. SQL中要插入到我的表"Friends"中的记录的集合.

Thanx



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

Thanx

推荐答案

declare @rowCount as int                 
select @rowCount= COUNT(*) from Friends
print @rowCount --to see the number of records.
if @rowCount<5
insert query


为了限制使用Insert,Update或Delete语句的记录数,请使用 TOP 关键字.
这是示例:

In order to restrict number of records with Insert, Update or Delete statements use TOP keyword.
Here is the example:

create table test (id int)

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

select * from test



输出将是:
1
2
3



Output will be:
1
2
3


这篇关于限制号码SQL中的记录数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆