以下是SQLSERVER的例子:
card表结构如下:
ID int NOT NULL IDENTITY (1, 1),
card_start char(16) NULL,
card_end char(16) NULL,
flag char(1) NULL
要把card_end删除
语句如下:
BEGIN TRANSACTION //开始事务
SET QUOTED_IDENTIFIER ON
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO
COMMIT
BEGIN TRANSACTION
CREATE TABLE dbo.Tmp_card
(
ID int NOT NULL IDENTITY (1, 1),
card_start char(16) NULL,
flag char(1) NULL
) ON "default"
GO //建新表
SET IDENTITY_INSERT dbo.Tmp_card ON //设自增字段不自增
GO
IF EXISTS(SELECT * FROM dbo.card)
EXEC('INSERT INTO dbo.Tmp_card(ID, card_start, flag)
SELECT ID, card_start, flag FROM dbo.card TABLOCKX')
GO //复制表内容
SET IDENTITY_INSERT dbo.Tmp_card OFF //自增有效
GO
DROP TABLE dbo.card //删除老表
GO
EXECUTE sp_rename 'dbo.Tmp_card', 'card' //命名
GO
COMMIT //执行事务