zoukankan      html  css  js  c++  java
  • SET IDENTITY_INSERT <Table Name> ON/OFF 转载

    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

  • 相关阅读:
    3.5星|津巴多《时间的悖论》:未来导向的人更有可能取得个人的成功,但帮助他人的可能性更小
    成功的销售必须在失败时也能快乐:4星|《哈佛商业评论》2018年第4期
    被取代的金融工作岗位越来越多:3星|《被人工智能操控的金融业》
    比特币和区块链是泡沫,但是短期内不会破:4星|《财经》2018年第7期
    点赞是当代可卡因:3星|《欲罢不能:刷屏时代如何摆脱行为上瘾》
    如何使用GetManifestResourceStream
    隐藏光标与获得光标2----获得光标所在的控件
    端口是否使用
    是否联网以及热点问题
    注册表操作
  • 原文地址:https://www.cnblogs.com/homchou/p/3274455.html
Copyright © 2011-2022 走看看