我收到以下代码的错误
ALTER TABLE ADM_Roles ALTER COLUMN RoleID int IDENTITY (1, 1)
关键字IDENTITY
附近的语法不正确。
答案 0 :(得分:6)
您无法将现有列更改为IDENTITY
列 - 您需要添加具有标识标记的新列:
ALTER TABLE dbo.ADM_Roles
ADD NewRoleID INT IDENTITY (1, 1)
如果需要,您可以稍后删除旧列并将新列重命名为旧名称:
ALTER TABLE dbo.ADM_Roles DROP COLUMN RoleID
EXEC sp_rename @objName = 'dbo.ADM_Roles.NewRoleID',
@newName = 'RoleID',
@objType = 'COLUMN'
答案 1 :(得分:0)
来自MSDN
You can't alter the existing columns for identity.
您有2个选项,
使用identity&创建一个新表删除现有表
- 醇>
使用identity&创建一个新列删除现有列但是 当这些列有任何约束/关系时要特别小心。
示例方法:
标识列将保存数字序列
Alter Table Names Add Id_new Int Identity(1,1)
Go
Alter Table Names Drop Column ID
Go
Exec sp_rename 'Names.Id_new', 'ID','Column'
答案 2 :(得分:0)
Tamnbien puede hacerlo a traves del diagrama de base de datos,modificas las propiedades de la tabla y agregar la columna que deseas que sea“identity”en la columna“identity column”
Eso me arreglo el problema de agregar identity a una columna existente。
答案 3 :(得分:-1)
你必须删除“int”这个词。
ALTER TABLE ADM_Roles ALTER COLUMN RoleId IDENTITY (1, 1);