Skip to content

Instantly share code, notes, and snippets.

@treffynnon
Created February 4, 2010 15:19
Show Gist options
  • Save treffynnon/294728 to your computer and use it in GitHub Desktop.
Save treffynnon/294728 to your computer and use it in GitHub Desktop.
T-SQL: Reset the primary keys
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