問題描述
我在 Visual Studio - VB.NET 中工作.
I'm working in Visual Studio - VB.NET.
我的問題是我想刪除 SQL Server 中的特定行,但我唯一的唯一列是一個自動遞增的標識.
My problem is that I want to delete a specific row in SQL Server but the only unique column I have is an Identity that increments automatically.
我的工作流程:
1.我在列中添加了一行(身份正在遞增,但我不知道數字)
2.我想刪除上一行
My process of work:
1. I add a row in the column (the identity is being incremented, but I don't know the number)
2. I want to delete the previous row
是否存在每條新記錄都有的唯一 ID?可能我的表有 2 條完全相同的記錄,只是序列(身份)不同.
Is there a sort of unique ID that every new record has? It's possible that my table has 2 exactly the same records, just the sequence (identity) is different.
任何想法如何處理這個問題?
Any ideas how to handle this problem?
推薦答案
SQL Server 有幾個函數可以返回最后一行的生成 ID,每個函數都有自己特定的優點和缺點.
SQL Server has a few functions that return the generated ID for the last rows, each with it's own specific strengths and weaknesses.
基本上:
@@IDENTITY
如果您不使用觸發器SCOPE_IDENTITY()
適用于您明確調用的代碼.IDENT_CURRENT(‘tablename’)
適用于所有范圍的特定表.
@@IDENTITY
works if you do not use triggersSCOPE_IDENTITY()
works for the code you explicitly called.IDENT_CURRENT(‘tablename’)
works for a specific table, across all scopes.
在幾乎所有情況下,SCOPE_IDENTITY()
都是您所需要的,并且使用它是一個好習慣,而不是其他選項.
此處.
In almost all scenarios SCOPE_IDENTITY()
is what you need, and it's a good habit to use it, opposed to the other options.
A good discussion on the pros and cons of the approaches is also available here.
這篇關于SQL Server 中是否每條記錄都有唯一的字段?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!