在sql server中删除后重置身份 [英] reset identity after delete in sql server

查看:67
本文介绍了在sql server中删除后重置身份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于sql的问题当我们删除一行假设102然后如果我插入然后插入的记录应该是102所以我想每次重置我的身份?帮助我......

i have one question about sql when we delete one row suppose 102 and then if i insert then that inserted record should be 102 so i want to reset my identity each time ? help me......

推荐答案

Hi Ankit,



你可以创建一个函数将接受表的名称作为参数。



该表的功能是识别缺少的id。



例如:

在我的表'Test'中我有Ids:1,2,3,5,6



现在当我调用该函数MyFunction('Test')时,这个函数应该返回4作为一个标识。



并且您可以使用返回的id。



例如:



INSERT INTO

测试

(Id,FirstName,LastName)

VALUES

(MyFunction('Test'),'aaa','bbb')
Hi Ankit,

You can create one function which will accept the name of the table as a parameter.

The functionality of that table is to identify the missing id.

For Example :
In my table 'Test' i have Ids : 1,2,3,5,6

Now when I call that function MyFunction('Test'), this function should return 4 as an identity.

And you can use that returned id.

ForExample :

INSERT INTO
Test
(Id,FirstName,LastName)
VALUES
(MyFunction('Test'),'aaa','bbb')


从表中删除行后,您必须重置表的标识为



DBCC CHECKIDENT( yourtable_name,reseed,101)



然后您的下一条记录带有身份102



http://www.dotnetbysatya.co.nr
after delete the row from table, you have to reset the identity of the table as

DBCC CHECKIDENT (yourtable_name, reseed, 101)

then your next record inserted with an identity 102

http://www.dotnetbysatya.co.nr


我接受它不是重置身份的好主意....... ::)
i accept that its not good idea to reset identity.......::)


这篇关于在sql server中删除后重置身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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