Created
February 4, 2010 15:19
-
-
Save treffynnon/294728 to your computer and use it in GitHub Desktop.
T-SQL: Reset the primary keys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BEGIN TRANSACTION | |
GO | |
CREATE TABLE dbo.Tmp_admin_accounts | |
( | |
account_id int NOT NULL IDENTITY (1, 1), | |
username varchar(20) NOT NULL, | |
passhash char(40) NOT NULL | |
) ON [PRIMARY] | |
GO | |
SET IDENTITY_INSERT dbo.Tmp_admin_accounts ON | |
GO | |
IF EXISTS(SELECT * FROM dbo.admin_accounts) | |
EXEC('INSERT INTO dbo.Tmp_admin_accounts (account_id, username, passhash) | |
SELECT account_id, username, passhash FROM dbo.admin_accounts WITH (HOLDLOCK TABLOCKX)') | |
GO | |
SET IDENTITY_INSERT dbo.Tmp_admin_accounts OFF | |
GO | |
DROP TABLE dbo.admin_accounts | |
GO | |
EXECUTE sp_rename N'dbo.Tmp_admin_accounts', N'admin_accounts', 'OBJECT' | |
GO | |
ALTER TABLE dbo.admin_accounts ADD CONSTRAINT | |
PK_admin_accounts PRIMARY KEY CLUSTERED | |
( | |
account_id | |
) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] | |
GO | |
COMMIT |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment