這是從SQL Server管理工作室輸出,但應該有一定的幫助。它涉及到創建臨時表,然後將現有數據複製到新表中。刪除原始表並重命名臨時表。如果表是空的,它不會這樣做,只能修改原始表。 SET IDENTITY_INSERT [table] ON/OFF
允許您在插入時設置標識列。
/* To prevent any potential data loss issues, you should review this script in detail before running it outside the context of the database designer.*/
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
GO
CREATE TABLE dbo.Tmp_category_types
(
id int NOT NULL IDENTITY (1, 1),
) ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_category_types SET (LOCK_ESCALATION = TABLE)
GO
SET IDENTITY_INSERT dbo.Tmp_category_types ON
GO
IF EXISTS(SELECT * FROM dbo.category_types)
EXEC('INSERT INTO dbo.Tmp_category_types (id, x)
SELECT CONVERT(int, id), x FROM dbo.category_types WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_category_types OFF
GO
DROP TABLE dbo.category_types
GO
EXECUTE sp_rename N'dbo.Tmp_category_types', N'category_types', 'OBJECT'
GO
ALTER TABLE dbo.category_types ADD CONSTRAINT
PK_category_types PRIMARY KEY CLUSTERED
(
id
) WITH(STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
COMMIT
嗨,我是在SQL Server中沒有任何專業知識,但是以爲你忘了「身份」在ALTER語句的結束。 – Babblo