我必须使用存储在另一个表中的计数器来更新一个 SQL 表中的列,并也更新该表
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您要插入一个表,然后更新下一个表,您可以使用 @@IDENTITY 为您提供第一个表中的自动增量 ID。
例如
@@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.
@@IDENTITY will give you the ID of the last inserted row.