https://www.cnblogs.com/daihuiquan/archive/2013/03/18/2956845.html

IDENT_CURRENT、IDENTITY、SCOPE_IDENTITY区别

概念解释

  • IDENT_CURRENT returns the last identity value generated for a specific table in any session and any scope.

  • IDENTITY returns the last identity value generated for any table in the current session, across all scopes.

  • SCOPE_IDENTITY returns the last identity value generated for any table in the current session and the current scope.

查找到有中文解释,但还是发现用英文解释,更容易理解。

IDENT_CURRENT,为指定表的所有会话和所有作用域生成的最后一个标识值;
IDENTITY,为当前会话的所有表和所有作用域的最后一个标识值;
SCOPE_IDENTITY,当前会话和当前作用域的所有表的最后一个标识值;

SQL说明

CREATE TABLE Test1(id int IDENTITY);
CREATE TABLE Test2(id int IDENTITY(100,1));
GO
CREATE TRIGGER Test1ins ON Test1 FOR INSERT
AS
BEGIN
INSERT Test2 DEFAULT VALUES
END;
GO
--End of trigger definition SELECT id FROM Test1;
--IDs empty.
SELECT id FROM Test2;
--ID is empty. --Do the following in Session 1
INSERT Test1 DEFAULT VALUES;
SELECT @@IDENTITY;
/*Returns the value 100. This was inserted by the trigger.*/ SELECT SCOPE_IDENTITY();
/* Returns the value 1. This was inserted by the
INSERT statement two statements before this query.*/ SELECT IDENT_CURRENT('Test2');
/* Returns value inserted into Test2, that is in the trigger.*/ SELECT IDENT_CURRENT('Test1');
/* Returns value inserted into Test1. This was the INSERT statement four statements before this query.*/

所有在执行insert 表A、B、C的事务时候,取IDENT_CURRENT("A")的时候,又没有及时将此ident插入相应记录D,而这个记录表D又要求插入的ident有唯一索引。这时就可能出现唯一索引重复插入失败,因为,在执行事务的时候其它会话,可能已经将此ident插入记录表D。所以这时候用SCOPE_IDENTITY更稳健一点。

补充

session和scope 在博客园找到的概念是这样的:

一个作用域 就是一个模块——存储过程、触发器、函数或批处理。因此,如果两个语句处于同一个存储过程、函数或批处理中,则它们位于相同的作用域中。

会话 一个用户连接产生的所有上下文信息。一个查询分析器窗口就是一个会话

但是一个用户connection代表一个session,这个好像不对。

A connection represents the external connection to the server (over a network or locally through shared memory for example).
A session represents a user process within SQL Server.
A connection may be associated with zero, one, or many sessions.

Take a look at the columns on sys.dm_exec_connections and sys.dm_exec_sessions. That should give you a feel for the differences.

sys.dm_exec_connections http://msdn.microsoft.com/zh-cn/library/ms181509.aspx

sys.dm_exec_sessions  http://msdn.microsoft.com/zh-cn/library/ms176013.aspx

IDENTITY、SCOPE_IDENTITY、IDENT_CURRENT的分析的更多相关文章

  1. 糟糕的@@identity,SCOPE_IDENTITY ,IDENT_CURRENT

    在某数据库里面,某甲用@@identity来获取最近插入的id值,当在多人环境,发生获取到null值的问题. 那么@@identity是否有存在的必要? 感觉像生个孩子,多了个指头. 有的数据库的ge ...

  2. IDENT_CURRENT ,@@identity,SCOPE_IDENTITY() 之间对比

    获取表的标识值,有3种比较常见的用法 IDENT_CURRENT ,@@identity,SCOPE_IDENTITY(),有啥不一样呢? 3个关键字在联机手册中的解释   IDENT_CURRENT ...

  3. [MSSQL]SCOPE_IDENTITY,IDENT_CURRENT以及@@IDENTITY的区别

    简单解释下SCOPE_IDENTITY函数,IDENT_CURRENT函数以及@@IDENTITY全局变量的区别 SCOPE_IDENTITY函数返回当前作用域内,返回最后一次插入数据表的标识,意思是 ...

  4. @@identity, scope_identity ident_current 的区别

  5. SQL SERVER-identity | @@identity | scope_identity

    主键自增 IDENTITY(1,1),MS SQL Server 使用 IDENTITY 关键字来执行 auto-increment 任务. 在上面的实例中,IDENTITY 的开始值是 1,每条新记 ...

  6. SCOPE_IDENTITY、IDENT_CURRENT 和 @@IDENTITY

    SCOPE_IDENTITY.IDENT_CURRENT 和 @@IDENTITY SQL Server 2000中,有三个比较类似的功能:他们分别是:SCOPE_IDENTITY.IDENT_CUR ...

  7. @@identity、scope_identity()、IDENT_CURRENT('tablename')函数的区别

    @@IDENTITY 和SCOPE_IDENTITY 返回在当前会话中的任何表内所生成的最后一个标识值.但是,SCOPE_IDENTITY 只返回插入到当前作用域中的值:@@IDENTITY 不受限于 ...

  8. Part 4 Identity Column in SQL Server

    Identity Column in SQL Server If a column is marked as an identity column, then the values for this ...

  9. ASP.NET Core Identity Hands On(2)——注册、登录、Claim

    上一篇文章(ASP.NET Core Identity Hands On(1)--Identity 初次体验)中,我们初识了Identity,并且详细分析了AspNetUsers用户存储表,这篇我们将 ...

随机推荐

  1. 【00NOIP普及组】计算器的改良(信息学奥赛一本通 1910)(洛谷 1022)

    [题目描述] NCL是一家专门从事计算器改良与升级的实验室,最近该实验室收到了某公司所委托的一个任务:需要在该公司某型号的计算器上加上解一元一次方程的功能.实验室将这个任务交给了一个刚进入的新手ZL先 ...

  2. 菜鸟教程C++(一)

    一.C++基本语法 C++程序可以定义为对象的集合,这些对象可以通过调用彼此的方法进行交互. 对象:对象具有状态和行为.例如:一只狗的状态:颜色.名称.品种等,行为:摇动.叫唤等.对象是类的实例. 类 ...

  3. 数据仓库DW、ODS、DM概念及其区别

    整体结构 在具体分析数据仓库之前先看下一下数据中心的整体架构以及数据流向   数据中心整体架构.png DB 是现有的数据来源,可以为mysql.SQLserver.文件日志等,为数据仓库提供数据来源 ...

  4. Netty集成Protobuf

    一.创建Personproto.proto 创建Personproto.proto文件 syntax = "proto2"; package com.example.protobu ...

  5. Java多个线程顺序打印数字

    要求 启动N个线程, 这N个线程要不间断按顺序打印数字1-N. 将问题简化为3个线程无限循环打印1到3 方法一: 使用synchronized 三个线程无序竞争同步锁, 如果遇上的是自己的数字, 就打 ...

  6. linux列出当前目录下的所有的目录?

    ###  列出当前目录下的所有目录: [root@localhost ~]# ls -ld * #列出所有的文件 drwxr-xr-x. root root Nov : elasticsearch d ...

  7. 深入学习c++--多线程编程(三)thread的两种死法

    1. 生成了一个线程,需要告诉编译器是否管理 必须告诉编译器是不管理还是管理,否则直接down了 #include <iostream> #include <thread> # ...

  8. k8s记录-yum本地仓库部署

    #1.安装插件yum install -y yum-plugin-downloadonly createrepo rsync #2.创建仓库目录mkdir -p /mirrors/centos#3.下 ...

  9. Centos7.3使用脚本自动静默安装oracle11.2.0.4数据库

    一直想着写一个脚本实现自动化安装oracle数据库.以下内容实验过几次了,可能还存在些小问题,如果在跑以下脚本中遇到问题,自己仔细排查即可 挣扎了好久,总算还是没实现,目前只能通过依次执行多个脚本来安 ...

  10. [LeetCode] 195. Tenth Line 第十行

    Given a text file file.txt, print just the 10th line of the file. Example: Assume that file.txt has ...