This command is from SQL Server.  This command is to enable the users to set their own value for IDENTITY Column in case they want to.  This has been the very useful for us.  We use it in MERGE and RDA when we send the client side inserts to server where we want to make sure to insert the row on server with the IDENTITY value generated by client.

Today in SQL Server CE (SSCE), we have no way to set our own value for IDENTITY column. Now there is a strong need for this feature in SQL Server Compact Edition.   Here are some more details:

SQL Server CE 2.0 supported setting the IDENTITY Column value and the apps migrated to SQL Mobile 3.0 have severely broken on this front.  Ex: Data Port Wizard from prime works

What has been given as a work around to customer right now is given below:

We can change the seed and step by “ALTER TABLE ALTER COLUMN”.  So, before doing an INSERT into SQL Server Compact database, one has to execute this “ALTER TABLE ALTER COLUMN” command to set the seed such a way that the next INSERT-ed row gets the value required.  But this is really a pain, because:

- ALTER TABLE ALTER COLUMN is a DDL.  Hence, the cursor needs to be closed and opened again.

- ALTER TABLE ALTER COLUMN is required for every INSERT.  Which means, an INSERT becomes:

a) Open Cursor b) ALTER TABLE c) Close Cursor d) Open Cursor e) INSERT Row f) Close Cursor

It is much like another session variable similar to “SET SHOWPLAN_XML ON”.  We skip identity checks, when this session variable for a table is ON.

USAGE-HOW-TO:

CREATE TABLE testIdentity (id int IDENTITY (1,1), name nvarchar(100));

INSERT INTO testIdentity(name) VALUES ('name1');                        è Row will be (1, name1)

INSERT INTO testIdentity(name) VALUES ('name2');                        è Row will be (2, name2)

SET IDENTITY_INSERT testIdentity ON;

INSERT INTO testIdentity (id, name) VALUES (10, 'name10');       è Row will be (10, name10)

INSERT INTO testIdentity (id, name) VALUES (20, 'name20');       è Row will be (20, name20)

SET IDENTITY_INSERT testIdentity OFF;

INSERT INTO testIdentity(name) VALUES ('name??');                       è Row will be (3, name??)

Here you can observe that SQL Server automatically uses 21 for the last row.  That is, SQL Server just leaves holes.  However,  we chose to not leave holes.  Hence, in SQL Server Compact the last row would get value ‘3’ for IDENTITY column.  This is basically for the reason that, when we are a merge replicated database we have a certain slot allocated to a subscriber and this range/slot is better managed by merge.  If we chose to behave as SQL Server here, IDENTITY_INSERT updates the AUTOINC_NEXT which MAY fall out of our allocated range which would pose a serious issue.  However, user can always get SQL Server behavior by “ALTER TABLE <Table Name> ALTER COLUMN <Column Name> <Datatype> IDENTITY(<New Seed>, <New Step>)”.

Thanks,

Laxmi Narsimha Rao ORUGANTI

refer:http://blogs.msdn.com/b/sqlservercompact/archive/2007/04/13/set-identity-insert-table-name-on-off.aspx

SET IDENTITY_INSERT <Table Name> ON/OFF 转载的更多相关文章

  1. DBCC CHECKIDENT 和SET IDENTITY_INSERT table OFF

    TRUNCATE TABLE [DBO].TRACKING_CODE_BASE_Jasmine DELETE FROM TRACKING_CODE_BASE_Jasmine 有同一张表,一次用trun ...

  2. kernel nf_conntrack: table full, dropping packet[转载]

    http://blog.yorkgu.me/2012/02/09/kernel-nf_conntrack-table-full-dropping-packet/ 综合:ip_conntrack就是li ...

  3. WPF中,Grid与Table的区别(英文)-转载

    原文地址:http://blog.csdn.net/johnsuna/article/details/1742799 How is Grid Different from Table?Table an ...

  4. EF架构~为BulkInsert引入SET IDENTITY_INSERT ON功能

    回到目录 在设计表结构时,我们通常将不是很在乎表现的主键设计成自增长的,大数据量用bigint,一般地用int就可以了,int就是C#里的Int32,它最大可以存储到2147483647,21亿,基本 ...

  5. 在线重定义(Rdefine Table online)

    二.        概念理解 在线重定义用于对表的逻辑或者物理结构的修改,而且在修改时不影响表的可用性与传统方式相比.当一个表被重定义时,会被锁定为exclusive mode很短一段时间,这段时间的 ...

  6. html表格导出Excel的一点经验心得(转载)

    最近在做统计功能,要求统计结果(表格)既能查看(BS系统,在浏览器查看),又能输出为excel文件.对于输出excel文件,在网上找到n种方案,因为还需查看,最终选择了统计结果输出为table,查看时 ...

  7. 把IDENTITY_INSERT 设置为 ON ,还不能插入数据问题

    IDENTITY_INSERT 为 ON 时 , 必须把需要插入的列名列出来 不然报错 正确例子: SET IDENTITY_INSERT  table(表名) ONinsert into table ...

  8. javascript实现汉诺塔动画效果

    javascript实现汉诺塔动画效果 当初以为不用html5也很简单,踩了javascript单线程的大坑后终于做出来了,没事可以研究下,对理解javascript的执行过程还是很有帮助的,代码很烂 ...

  9. Identity标识列

    SQL Server中,经常会用到Identity标识列,这种自增长的字段操作起来的确是比较方便.但它有时还会带来一些麻烦. 示例一 :当表中被删除了某些数据的时候,自增长列的编号就不再是一个连线的数 ...

随机推荐

  1. [移动应用安全]移动应用安全培训PPT

    这是年初给公司做的移动应用安全培训,主要是针对开发的兄弟们,让大家对目前的移动领域的安全形势有个了解,以及不正确的开发所带来的危害,另外还配了个详细的word版的移动应用开发指南,后面一起附上:).

  2. this和$(this)的关系

    环境关键字this引用的是DOM元素 $(this)是jQuery对象 下面点击按钮分别alert一下 alert(this); alert($(this)); 获取DOM对象的属性id,可以 $(t ...

  3. 【Morn UI系列教程】Morn简介及使用教程

    网页游戏开发的一大部分工作是在和UI制作上,一个好的工具及框架能使开发事半功倍,Adobe自带flash IDE和Flex各有不足. Morn UI学习了Flash IDE使用的方便性(比如图层功能, ...

  4. PTCSolution 关注

      2013.12.28更新: 经过几次整理和再次租赁下线,点击数明显增加,现在995下线点击数3413.再将2页半的点击数低的下线替换掉,AVG肯定超过4. 这么短的时间整理效果如此明显是我没有想到 ...

  5. Kubernetes下的应用监控解决方案

    所谓应用监控,更多的是基于java jvm的监控,因为公司运行的中间件大部分都是基于tomcat,Springboot,SpringCloud,当然也必须支持WebLogic.在Kubernetes现 ...

  6. android4.1 JELLY_BEAN:All WebView methods must be called on the same thread[问题已解决]

    11-06 18:29:15.582: W/WebView(27807): java.lang.Throwable: A WebView method was called on thread 'Ja ...

  7. spring的6个不同的功能模块

    Spring自带的jar文件 Spring模块组成图 Spring的主要模块各自是核心Spring容器,spring的AOP模块,数据訪问与集成,web和远程调用,測试. 核心spring容器: 容器 ...

  8. iOS:UIView的CALayer基本演练

    UIView的CALayer基本演练的属性和注意事项: 在UIView中创建一个按钮UIButton,然后设置UIButton的Layer属性 –圆角.边框.阴影及3D形变属性 注意: 1.在UIVi ...

  9. 【转载】【Todo】银弹与我们的职业

    看到一段文字,不得不单独拎出来. 然后再借用一下g9老大的<银弹和我们的职业>中的话: 银弹和我们的职业发展有什么相干?很简单:我们得把时间用于学习解决本质困难.新技术给高手带来方便.菜鸟 ...

  10. centOS6.5 Hadoop1.0.4安装

    前段时间去培训,按照教程装了一遍Hadoop.回来又重新装一次,捋下思路,加深理解. 基本配置如下,三个节点,一个namenode,两个datanode. Namenode 192.168.59.14 ...