How to reset identity seed in Sql Azure

坚强是说给别人听的谎言 提交于 2019-12-17 16:55:24

问题


I have tried the following in order to reset my Identity Seed of a column:

DBCC CHECKIDENT ('dbo.Stuff', RESEED, 0)

This does not work in sql azure, I was wondering what would be the best way to go about doing this. Drop and recreate table?


回答1:


As you have seen, DBCC CHECKIDENT is not currently supported in Windows Azure SQL Database. The most complete way to reseed the identity would be to create a new table with the same structure, and set the identity to the Reseed value IDENTITY(reseedval,1), load existing records from the original table using SET IDENTITY_INSERT tablename ON, then drop the old table, and rename the new one. Reset permissions, reset constraints, etc.




回答2:


I think @haldyr's answer was probably unfairly deleted as its correct.

The latest version of SQL Azure (v12) DOES support DBCC CHECKIDENT (and a bunch of others) however if your azure database server isn't brand new you will need to upgrade.

Upgrading is easy, go to your azure DB Server on the new portal (the actual server not the DB itself) and click the big Latest Update button.

NOTE: Be aware there are a bunch of caveats to upgrading (Like some older stuff doesn't support it), so its worth reading the article http://azure.microsoft.com/en-us/documentation/articles/sql-database-preview-whats-new/#V12AzureSqlDbPreviewGaTable




回答3:


I have recently come up against this problem myself. I resolved it by inserting and deleting items in the table until I reached the desired seed value.

In the example below, this will reseed the Users table so the next new item will have Id of 3500.

BEGIN TRANSACTION
GO

DECLARE @max_id int

SELECT @max_id = max([Id]) FROM users

WHILE (@max_id < 3499)
BEGIN
    INSERT INTO Users (Username, Password) VALUES ('','')
    SELECT @max_id = max([Id]) FROM Users
    DELETE FROM Users WHERE [Id] = @max_id
END

COMMIT

Hope this helps.



来源:https://stackoverflow.com/questions/12415434/how-to-reset-identity-seed-in-sql-azure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!