identity in sql server 批量插入history
- The
@@identityfunction 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 aselect...intoquery.
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.
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的更多相关文章
- SQL Server 批量插入数据的两种方法
在SQL Server 中插入一条数据使用Insert语句,但是如果想要批量插入一堆数据的话,循环使用Insert不仅效率低,而且会导致SQL一系统性能问题.下面介绍 SQL Server支持的两种批 ...
- SQL Server 批量插入数据的两种方法(转)
此文原创自CSDN TJVictor专栏:http://blog.csdn.net/tjvictor/archive/2009/07/18/4360030.aspx 在SQL Server 中插入一条 ...
- 转:SQL Server 批量插入数据的两种方法
在SQL Server 中插入一条数据使用Insert语句,但是如果想要批量插入一堆数据的话,循环使用Insert不仅效率低,而且会导致SQL一系统性能问题.下面介绍SQL Server支持的两种批量 ...
- SQL Server 批量插入
使用场景 在项目中,涉及到数据库表变更时,可能会需要将一个或多个表中的数据迁移到另一个表中. 这时用sql去执行会很方便! sql语句 //SQL批量插入 create table #ttableNa ...
- SQL Server 批量插入数据方案 SqlBulkCopy 的简单封装,让批量插入更方便
一.Sql Server插入方案介绍 关于 SqlServer 批量插入的方式,有三种比较常用的插入方式,Insert.BatchInsert.SqlBulkCopy,下面我们对比以下三种方案的速度 ...
- SQL server 批量插入和更新数据
批量插入数据 insert into A表数据库名.[dbo].A(a,b,c) (select a,b,c from B表数据库名.[dbo].B) 批量更新数据 根据身份证第二位更新性别 upda ...
- SQL Server 批量插入数据的方法
运行下面的脚本,建立测试数据库和表. --Create DataBase create database BulkTestDB; go use BulkTestDB; go --Create Tabl ...
- SQL SERVER 批量插入记录
--create function insertData(@count as int,@tsn as bigint,@id as int) --as --begin SET IDENTITY_INSE ...
- SQL Server 批量插入数据
请看代码: 创建表值参数类型: 请看代码:
随机推荐
- PDO获取数据乱码的解决方法
确保PHP文件编码格式为UTF8 确保数据字段格式为UTF8 PDO中设置编码格式,有如下三种方式: 方式1: 写在初始化dsn中 define( 'DB_DSN', 'mysql:host=loca ...
- MOOC推荐及三门基础学科
top1:学堂在线 http://www.xuetangx.com/ top2:网易云课堂 http://study.163.com/ top3:coursera https://www.course ...
- python排序sorted与sort比较 (转)
Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列. sorted(iterable,key=None,revers ...
- javaee IO流作业02
package Zy; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.Fil ...
- BOS工具之BOS应用框架
大纲: 应用框架概述,bos应用框架总体,bos应用框架详细设计,代码结构以及常用应用,开发常用接口 框架(Framework)是整个或部分系统的可重用设计,表现为一组抽象组件及组件实例间交互的 ...
- Idea 类注释和方法注释
类注释 先打开Settings > Editor > File and Code Templates Includes Includes File Header 再随机新建个类就有类注释 ...
- Ali-Tomcat在eclipse多开的解决方法
关于如何在eclipse配置Ali-Tomcat https://help.aliyun.com/document_detail/99410.html?spm=a2c4g.11186623.6.609 ...
- Set Time, Date Timezone in Linux from Command Line or Gnome | Use ntp
https://www.garron.me/en/linux/set-time-date-timezone-ntp-linux-shell-gnome-command-line.html Set ti ...
- ExtJs之Ajax模式的表单数据加载
简单: <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-e ...
- [bzoj2529][Poi2011]Sticks_贪心
Sticks bzoj-2529 Poi-2011 题目大意:给你n根木棒,每种木棒有长度和颜色,颜色共有k种,求满足条件的3根木棒使得这3根木棒颜色互不相同且可以围成三角形. 注释:$1\le n ...