使 Microsoft® SQL Server™ 在返回指定的行数之后停止处理查询。
语法
SET ROWCOUNT { number | @number_var }
B. 使用一般语法查找标识值中的差距
下面的示例显示一般的语法,当删除数据时,可以使用该语法查找标识值中的差距。
说明 下面的 Transact-SQL 脚本中的第一部分只用作示范说明。可以运行以下面的注释开始的 Transact-SQL 脚本:- - Create the img table.
-- Here is the generic syntax for finding identity value gaps in data.
-- This is the beginning of the illustrative example.
SET IDENTITY_INSERT tablename ON
DECLARE @minidentval column_type
DECLARE @nextidentval column_type
SELECT @minidentval = MIN(IDENTITYCOL) FROM tablename
IF @minidentval = IDENT_SEED('tablename')SELECT @nextidentval = MIN(IDENTITYCOL) + IDENT_INCR('tablename')
FROM tablename t1
WHERE IDENTITYCOL BETWEEN IDENT_SEED('tablename') AND
MAX(column_type) AND
NOT EXISTS (SELECT * FROM tablename t2
WHERE t2.IDENTITYCOL = t1.IDENTITYCOL +
IDENT_INCR('tablename'))ELSE
SELECT @nextidentval = IDENT_SEED('tablename')SET IDENTITY_INSERT tablename OFF-- Here is an example to find gaps in the actual data.-- The table is called img and has two columns: the first column-- called id_num, which is an increasing identification number, and the-- second column called company_name.-- This is the end of the illustration example.-- Create the img table.-- If the img table already exists, drop it.-- Create the img table.IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLESWHERE TABLE_NAME = 'img')DROP TABLE imgGOCREATE TABLE img (id_num int IDENTITY(1,1), company_name sysname)INSERT img(company_name) VALUES ('New Moon Books')INSERT img(company_name) VALUES ('Lucerne Publishing')-- SET IDENTITY_INSERT ON and use in img table.SET IDENTITY_INSERT img ONDECLARE @minidentval smallintDECLARE @nextidentval smallintSELECT @minidentval = MIN(IDENTITYCOL) FROM imgIF @minidentval = IDENT_SEED('img')SELECT @nextidentval = MIN(IDENTITYCOL) + IDENT_INCR('img')FROM img t1WHERE IDENTITYCOL BETWEEN IDENT_SEED('img') AND 32766 ANDNOT EXISTS (SELECT * FROM img t2WHERE t2.IDENTITYCOL = t1.IDENTITYCOL + IDENT_INCR('img'))ELSESELECT @nextidentval = IDENT_SEED('img')SET IDENTITY_INSERT img OFF
本文介绍如何使用Transact-SQL脚本查找Microsoft SQL Server中由于删除数据导致的身份值序列中的空缺。通过设置示例展示了如何定位这些间隙,并提供了一段具体的SQL脚本来演示整个过程。
1008

被折叠的 条评论
为什么被折叠?



