多次 SQL 插入后标识列值的依赖性

发布于 2024-07-30 07:36:38 字数 222 浏览 1 评论 0原文

如果下一个是正确的: 有多次插入的 SQL 字符串(使用存储过程):

“EXEC SPInsertData ... EXEC SPInsertData ... EXEC SPInsertData ...”

每条新记录的标识列中的 id 自动递增,小于下一个的 id。

例如,执行给定的SQL字符串后,第一条记录的id小于第二条记录的id,并且其id小于第三条记录的id?

If the next is right:
There are SQL string with multiple inserts (using a stored procedure):

"EXEC SPInsertData ... EXEC SPInsertData ... EXEC SPInsertData ..."

The id in identity column, which is auto incremented, of every new record is smaller than the id of the next one.

E.g. after executing the given SQL string the id of the first record is smaller than id of the second record, and its id is smaller than id of the third record?

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

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

发布评论

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

评论(2

腻橙味 2024-08-06 07:36:38

是的,如果它是正确的自动递增标识列

Yes, if it's an auto-incrementing identity column that is correct

无远思近则忧 2024-08-06 07:36:38

本质上,自动增量会随着每次插入而前进增量量。

MS SQL Server 提供了一种创建相反顺序的方法。 看看这里

create table #test
(
    TestId INT IDENTITY (2, -1),
    DateTimeStamp   DateTime
)
GO
INSERT INTO #test (DateTimeStamp) Values (GETDATE());
INSERT INTO #test (DateTimeStamp) Values (GETDATE());
INSERT INTO #test (DateTimeStamp) Values (GETDATE());
INSERT INTO #test (DateTimeStamp) Values (GETDATE());
INSERT INTO #test (DateTimeStamp) Values (GETDATE());
INSERT INTO #test (DateTimeStamp) Values (GETDATE());
INSERT INTO #test (DateTimeStamp) Values (GETDATE());
INSERT INTO #test (DateTimeStamp) Values (GETDATE());
GO
SELECT * FROM #test

结果:

TestId      DateTimeStamp
2           2009-07-28 15:02:09.200
1           2009-07-28 15:02:09.200
0           2009-07-28 15:02:09.200
-1          2009-07-28 15:02:09.200
-2          2009-07-28 15:02:09.203
-3          2009-07-28 15:02:09.203
-4          2009-07-28 15:02:09.203
-5          2009-07-28 15:02:09.207

By nature, autoincrements go forward with every insert by the amount of the increment.

MS SQL Server offers a way to create a reverse order. Take a look here

create table #test
(
    TestId INT IDENTITY (2, -1),
    DateTimeStamp   DateTime
)
GO
INSERT INTO #test (DateTimeStamp) Values (GETDATE());
INSERT INTO #test (DateTimeStamp) Values (GETDATE());
INSERT INTO #test (DateTimeStamp) Values (GETDATE());
INSERT INTO #test (DateTimeStamp) Values (GETDATE());
INSERT INTO #test (DateTimeStamp) Values (GETDATE());
INSERT INTO #test (DateTimeStamp) Values (GETDATE());
INSERT INTO #test (DateTimeStamp) Values (GETDATE());
INSERT INTO #test (DateTimeStamp) Values (GETDATE());
GO
SELECT * FROM #test

Results:

TestId      DateTimeStamp
2           2009-07-28 15:02:09.200
1           2009-07-28 15:02:09.200
0           2009-07-28 15:02:09.200
-1          2009-07-28 15:02:09.200
-2          2009-07-28 15:02:09.203
-3          2009-07-28 15:02:09.203
-4          2009-07-28 15:02:09.203
-5          2009-07-28 15:02:09.207
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文