如何在不插入值的情况下增加标识列?

发布于 2024-08-06 03:42:08 字数 821 浏览 2 评论 0原文

首先,我知道 这个问题没有得到回答,因为OP真正想做的不是增加身份列,


我有一个当前种子值为x的身份列,我想重新播种它到 x+1 (即我希望我的身份列直接从 x 跳转到 x+2。

我知道我可以使用以下 command

create procedure IncrementSeedValue
(
@TableName varchar(255),
@IncrementValue int
)
as

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
BEGIN TRANSACTION;
    declare @v bigint
    select @v = IDENT_CURRENT(@TableName)+@IncrementValue
    DBCC CHECKIDENT (@TableName, RESEED, @v )
COMMIT TRANSACTION;
go

但是,我有几个问题:

  • 这里的隔离级别“可序列化”是否足够?
  • 如果我使用 SQL Server,这会导致问题吗?镜像还有
  • 其他我应该注意的陷阱吗?

First, I'm aware of this question which didn't get answered because what the OP was really trying to do was'nt incrementing an identity column


I've got an identity column with a current seed value of x, and I would like to reseed it to x+1 (ie I want my identity column to jump directly from x to x+2.

I know I can do that using the following command

create procedure IncrementSeedValue
(
@TableName varchar(255),
@IncrementValue int
)
as

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
BEGIN TRANSACTION;
    declare @v bigint
    select @v = IDENT_CURRENT(@TableName)+@IncrementValue
    DBCC CHECKIDENT (@TableName, RESEED, @v )
COMMIT TRANSACTION;
go

However, I've got a few questions :

  • Is the isolation level "serializable" adequate here?
  • Would that lead to problem if I'm using SQL Server mirroring
  • Are there other pitfalls I should be aware of ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鲜肉鲜肉永远不皱 2024-08-13 03:42:08

每个 INSERT 都会消耗一个 IDENTITY 值。

我要么插入一个值并立即删除它,要么插入该值并发出回滚。

无论哪种情况,IDENTITY 值都会被消耗。

我认为 DBCC 命令不应该在“正常”代码中使用,而应该仅用于管理任务。

或者,如果您可以使用 SET IDENTITY_INSERT ON 并计算跳过值的下一个值应该是什么。

An IDENTITY value is consumed for every INSERT.

I would either INSERT a value and immediately DELETE it, or INSERT the value and issue a ROLLBACK.

In either case, the IDENTITY value will be consumed.

I don't feel that DBCC commands should be used in "normal" code and should be reserved for administrative tasks only.

Or, if you can use SET IDENTITY_INSERT ON and calculate what the next value with the skipped value should be.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文