我必须使用存储在另一个表中的计数器来更新一个 SQL 表中的列,并也更新该表

发布于 2024-08-25 01:25:47 字数 225 浏览 2 评论 0原文

我正在使用 SQL Server 2005(用于测试)& 2007 年(用于生产)。

我必须使用另一个表中的“最后记录 ID”列,在现有列中向表中的所有记录添加唯一的记录 ID。因此,我将对表进行某种 UPDATE 操作,但我必须从另一个表中获取“最后一条记录 ID”,递增它,更新该表,然后更新我的记录。

谁能给我一个如何做到这一点的例子?其他用户也可能会增加计数器。

I'm using SQL server 2005 (for testing) & 2007 (for production).

I have to add a unique record ID to all the records in my table, in an existing column, using a "last record ID" column from another table. So, I'm going to do some sort of UPDATE of my table, but I have to get the "last record ID" from the other table, increment it, update THAT table and then update my record.

Can anyone give me an example of how to do this? Other users may be incrementing the counter also.

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

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

发布评论

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

评论(1

够运 2024-09-01 01:25:47

如果您要插入一个表,然后更新下一个表,您可以使用 @@IDENTITY 为您提供第一个表中的自动增量 ID。

例如

INSERT INTO table1 (description) VALUES ('blah');
UPDATE table2 SET (tabel1ID = @@IDENTITY) WHERE condition

@@IDENTITY 将为您提供最后插入行的ID。

if you are INSERTING into one table and then UPDATE(ing) the next table you can use @@IDENTITY to give you the auto increment ID from the first table.

E.g.

INSERT INTO table1 (description) VALUES ('blah');
UPDATE table2 SET (tabel1ID = @@IDENTITY) WHERE condition

@@IDENTITY will give you the ID of the last inserted row.

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