使用SQL SERVER修改表中的现有列
问题描述:
我的桌子上有10条记录.我想用标识修改表列
喜欢,
ALTER TABLE ALTER COLUMN整数标识(10,1)
表中已经插入了10个值.
我的错误:
关键字"IDENTITY"附近的语法不正确.
I Have 10 records in my table. i want to modify the table column with identity
like,
ALTER TABLE ALTER COLUMN INT IDENTITY (10, 1)
Already 10 Values are inserted in Table.
My Error:
Incorrect syntax near the keyword ''IDENTITY''.
答
Alter Table Names
Add Id_new Int Identity(10, 1)
Go
Alter Table Names Drop Column ID
Go
Exec sp_rename 'Names.Id_new', 'ID', 'Column'
请检查.
http://stackoverflow.com/questions/1049210/adding-an-identity- to-an-existing-column [ ^ ]
Please check.
http://stackoverflow.com/questions/1049210/adding-an-identity-to-an-existing-column[^]
您忘记了列名-它应该在单词"COLUMN"和"INT"之间
You forgot the column name - it should be between the words "COLUMN" and "INT"
删除旧列并添加新列.
Remove the old column and add new one.
--SQL Script:
ALTER TABLE Users DROP PK_Users
GO
ALTER TABLE Users DROP COLUMN UserID
GO
ALTER TABLE Users ADD UserID int NOT NULL IDENTITY(1,1)
GO
ALTER TABLE Users ADD CONSTRAINT PK_Users PRIMARY KEY CLUSTERED ([UserID] ASC)
GO