Does every record has an unique field in SQL Server?(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 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 中是否每条记录都有唯一的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SQL Server 中是否每条记录都有唯一的字段?


基础教程推荐
- SQL Server 2016更改对象所有者 2022-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01