我是新的SQL世界 我既不想重建整个表,也不想添加新列。
答案 0 :(得分:1)
如果要更改现有的表列,可以尝试: 用tmp_yourtablename替换Tmp_example1 用yourtablename替换example1 用你的列名替换eid
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_example1
(
eid INT NOT NULL IDENTITY (1, 1)
) ON [PRIMARY] GO
SET IDENTITY_INSERT dbo.Tmp_example1 ON
GO
IF EXISTS(SELECT * FROM dbo.example1)
EXEC('INSERT INTO dbo.Tmp_example1 (eid)
SELECT eid FROM dbo.example1 WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_example1 OFF
GO
DROP TABLE dbo.example1
GO
EXECUTE sp_rename N'dbo.Tmp_example1', N'example1', 'OBJECT'
GO
COMMIT
答案 1 :(得分:0)
//删除列
Alter TABLE [dbo].[t_name]
drop column [columnname]
//使用标识和以前的列名创建一个新列
Alter TABLE [dbo].[t_name]
add [columnname] [int] IDENTITY(1,1) NOT NULL