IDENTITY_INSERT is set to OFF - How to turn it ON?

前端 未结 7 1356
-上瘾入骨i
-上瘾入骨i 2020-12-12 18:05

I have a deleted file archive database that stores the ID of the file that was deleted, I want the admin to be able to restore the file (as well as the same ID for linking

相关标签:
7条回答
  • 2020-12-12 18:11

    Add this line above you Query

    SET IDENTITY_INSERT tbl_content ON
    
    0 讨论(0)
  • 2020-12-12 18:12

    The Reference: http://technet.microsoft.com/en-us/library/aa259221%28v=sql.80%29.aspx

    My table is named Genre with the 3 columns of Id, Name and SortOrder

    The code that I used is as:

    SET IDENTITY_INSERT Genre ON
    
    INSERT INTO Genre(Id, Name, SortOrder)VALUES (12,'Moody Blues', 20) 
    
    0 讨论(0)
  • 2020-12-12 18:16

    Reminder

    SQL Server only allows one table to have IDENTITY_INSERT property set to ON.

    This does not work:

    SET IDENTITY_INSERT TableA ON
    SET IDENTITY_INSERT TableB ON
    ... INSERT ON TableA ...
    ... INSERT ON TableB ...
    SET IDENTITY_INSERT TableA OFF
    SET IDENTITY_INSERT TableB OFF
    

    Instead:

    SET IDENTITY_INSERT TableA ON
    ... INSERT ON TableA ...
    SET IDENTITY_INSERT TableA OFF
    SET IDENTITY_INSERT TableB ON
    ... INSERT ON TableB ...
    SET IDENTITY_INSERT TableB OFF
    
    0 讨论(0)
  • 2020-12-12 18:23

    I believe it needs to be done in a single query batch. Basically, the GO statements are breaking your commands into multiple batches and that is causing the issue. Change it to this:

    SET IDENTITY_INSERT tbl_content ON
    /* GO */
    
    ...insert command...
    
    SET IDENTITY_INSERT tbl_content OFF
    GO
    
    0 讨论(0)
  • 2020-12-12 18:33

    Shouldn't you be setting identity_Insert ON, inserting the records and then turning it back off?

    Like this:

    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    SET IDENTITY_INSERT tbl_content ON
    GO
    
    ALTER procedure [dbo].[spInsertDeletedIntoTBLContent]
    @ContentID int, 
    SET IDENTITY_INSERT tbl_content ON
    ...insert command...
    SET IDENTITY_INSERT tbl_content OFF
    
    0 讨论(0)
  • 2020-12-12 18:33

    Add set off also

     SET IDENTITY_INSERT Genre ON
    
        INSERT INTO Genre(Id, Name, SortOrder)VALUES (12,'Moody Blues', 20) 
    
        SET IDENTITY_INSERT Genre  OFF
    
    0 讨论(0)
提交回复
热议问题