https://stackoverflow.com/questions/1920558/what-is-the-difference-between-scope-identity-identity-identity-and-ide

  • The @@identity function returns the last identity created in the same session.
  • The scope_identity() function returns the last identity created in the same session and the same scope.
  • The ident_current(name) returns the last identity created for a specific table or view in any session.
  • The identity() function is not used to get an identity, it's used to create an identity in a select...into query.

The session is the database connection. The scope is the current query or the current stored procedure.

A situation where the scope_identity() and the @@identity functions differ, is if you have a trigger on the table. If you have a query that inserts a record, causing the trigger to insert another record somewhere, the scope_identity() function will return the identity created by the query, while the @@identity function will return the identity created by the trigger.

So, normally you would use the scope_identity() function.

https://stackoverflow.com/questions/13399565/is-there-any-way-to-use-scope-identity-if-using-a-multiple-insert-statement

No, SCOPE_IDENTITY() only gives you the one, latest inserted IDENTITY value. But you could check out the OUTPUT clause of SQL Server ....

DECLARE @IdentityTable TABLE (SomeKeyValue INT, NewIdentity INT)

INSERT INTO [MyTable]
OUTPUT Inserted.Keyvalue, Inserted.ID INTO @IdentityTable(SomeKeyValue, NewIdentity)
VALUES (''), (''), ('')

Once you've run your INSERT statement, the table variable will hold "some key value" (for you, to identify the row) and the newly inserted ID values for each row inserted. Now go crazy with this! :-)

其中Inserted是和Output有关系的

UPDATE OUTPUT into a variable

Because an update can affect multiple rows, it requires a table to store its results:

declare @ids table (id int);

UPDATE Foo
SET Bar = 1
OUTPUT INSERTED.Id INTO @ids
WHERE Baz = 2

If you're sure only one row will be affected, you can pull out the id like:

declare @id int
select top 1 @id = id
from @ids

identity in sql server 批量插入history的更多相关文章

  1. SQL Server 批量插入数据的两种方法

    在SQL Server 中插入一条数据使用Insert语句,但是如果想要批量插入一堆数据的话,循环使用Insert不仅效率低,而且会导致SQL一系统性能问题.下面介绍 SQL Server支持的两种批 ...

  2. SQL Server 批量插入数据的两种方法(转)

    此文原创自CSDN TJVictor专栏:http://blog.csdn.net/tjvictor/archive/2009/07/18/4360030.aspx 在SQL Server 中插入一条 ...

  3. 转:SQL Server 批量插入数据的两种方法

    在SQL Server 中插入一条数据使用Insert语句,但是如果想要批量插入一堆数据的话,循环使用Insert不仅效率低,而且会导致SQL一系统性能问题.下面介绍SQL Server支持的两种批量 ...

  4. SQL Server 批量插入

    使用场景 在项目中,涉及到数据库表变更时,可能会需要将一个或多个表中的数据迁移到另一个表中. 这时用sql去执行会很方便! sql语句 //SQL批量插入 create table #ttableNa ...

  5. SQL Server 批量插入数据方案 SqlBulkCopy 的简单封装,让批量插入更方便

    一.Sql Server插入方案介绍 关于 SqlServer 批量插入的方式,有三种比较常用的插入方式,Insert.BatchInsert.SqlBulkCopy,下面我们对比以下三种方案的速度 ...

  6. SQL server 批量插入和更新数据

    批量插入数据 insert into A表数据库名.[dbo].A(a,b,c) (select a,b,c from B表数据库名.[dbo].B) 批量更新数据 根据身份证第二位更新性别 upda ...

  7. SQL Server 批量插入数据的方法

    运行下面的脚本,建立测试数据库和表. --Create DataBase create database BulkTestDB; go use BulkTestDB; go --Create Tabl ...

  8. SQL SERVER 批量插入记录

    --create function insertData(@count as int,@tsn as bigint,@id as int) --as --begin SET IDENTITY_INSE ...

  9. SQL Server 批量插入数据

    请看代码: 创建表值参数类型: 请看代码:

随机推荐

  1. Spring Cloud (3) 服务消费者-Ribbon

    在上一篇中使用LoadBalancerClient接口实现了获取某个服务的具体实例,并根据实例信息发起服务接口消费请求.但是这样的做法需要我们手工的区编写服务选取.连接拼接等繁琐的工作,对于开发人员来 ...

  2. 6月10遇到的bug

    3.遇到的BUG        1.@Service(version = "1.0.0", interfaceClass = TransactionItemService.clas ...

  3. js仿新浪游戏频道导航条

    js仿新浪游戏频道导航条 在线演示本地下载

  4. Java_Web之神奇的Ajax

    为什么使用Ajax? 无刷新:不刷新整个页面,只刷新局部 无刷新的好处 提供类似C/S的交互效果,操作更方面 只更新部分页面,有效利用带宽   什么是Ajax?   XMLHttpRequest常用方 ...

  5. css基础四

    过渡属性 下面的表格列出了所有的转换属性: 属性 描述 CSS transition 简写属性,用于在一个属性中设置四个过渡属性. 3 transition-property 规定应用过渡的 CSS ...

  6. Python-通过configparser读写配置文件

    Python读写配置文件: 1.创建配置文件(文件名以.conf或.ini结束的文件表示配置文件) 2.导入所需模块 OS, configparser >>> import os & ...

  7. react性能调谐与diff算法

    一个页面其实就相当于是一颗dom树,里面有很多它的子节点,然后你每次去操作一个事件,它都会生成一个虚拟dom,它会跟上一个虚拟dom进行比对,这里运用的算法叫做diff算法,当它找到需要改变的组件的时 ...

  8. 构造函数+原型的js混合模式

    function Parent(){ this.name = "李小龙"; this.age = "30"; };Parent.prototype.lev=fu ...

  9. Oracle经验积累

    Oracle 常用Funtion ----行转列 开始---- select u_id, wmsys.wm_concat(goods) goods_sum from shopping group by ...

  10. 【scoi2009】围豆豆(最短路模型)

    洛谷题面:https://www.luogu.org/problemnew/show/P2566 由每个豆子引一条射线,与射线交点个数为奇数相当于多边形围住了它,这样可以定义一个状态f[x][y][s ...