SQL Server 中是否每条记录都有唯一的字段?

我在 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 triggers
  • SCOPE_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.

相关文章