Update ANSI_NULLS option in an existing table

后端 未结 3 1793
独厮守ぢ
独厮守ぢ 2021-01-08 00:44

In our database there is a table which is created with ANSI_NULLS OFF. Now we have created a view using this table. And we want to add a clustered index for thi

3条回答
  •  梦谈多话
    2021-01-08 00:53

    I tried the SWITCH option recommended above but was unable to RESEED the identity. I could not find out why.

    I used the following alternative approach instead:

    1. Create database snapshot for the database that contains the table
    2. Script table definition of the table you intend to update
    3. Delete the table that you intend to update (Make sure the database snapshot is successfully created)
    4. Update SET ANSI NULLs from OFF to ON from the script obtained from step 2 and run updated script. Table is now recreated.
    5. Populate data from database snapshot to your table: SET IDENTITY_INSERT TABLE_NAME ON INSERT INTO TABLE_NAME (PK, col1, etc.) SELECT PK, col1, etc. FROM [Database_Snapshot].dbo.TABLE_NAME SET IDENTITY_INSERT TABLE_NAME OFF
    6. Migrate non clustered index manually (get script from database snapshot)

    Using the above:

    • I did not have to worry about constraints and keys since table/constraint names always remain the same (I do not need to rename anything)
    • I have a backup of my data (the snapshot) which I can rely on to double check that nothing is missing.
    • I do not need to reseed the identity

    I realize deleting table may not always be straightforward if table is referenced in other tables. That was not the case for me in this instance.. I was lucky.

提交回复
热议问题