0

我现在有这张桌子...

CREATE TABLE [dbo].[DatosLegales](
    [IdCliente] [int] NOT NULL,
    [Nombre] [varchar](max) NULL,
    [RFC] [varchar](13) NULL,
    [CURP] [varchar](20) NULL,
    [IMSS] [varchar](20) NULL,
    [Calle] [varchar](100) NULL,
    [Numero] [varchar](10) NULL,
    [Colonia] [varchar](100) NULL,
    [Pais] [varchar](50) NULL,
    [Estado] [varchar](50) NULL,
    [Ciudad] [varchar](50) NULL,
    [CodigoPostal] [varchar](10) NULL,
    [Telefono] [varchar](13) NULL,
    [TipoEmpresa] [varchar](20) NULL,
    [Tipo] [varchar](20) NULL,
)

IdCliente 既是主键(不可索引),又与另一个表相关。我现在想要的是添加一个名为 IdDatoLegal 的列......我希望它成为新的主键,成为可索引的,对于已经添加的记录,我需要使用以下值更新它们:1、2、 3, 4....直到最后一行...

我正在开始查询,但我不知道如何继续...

ALTER TABLE DatosLegales DROP PRIMARY KEY;
ALTER TABLE DatosLegales ADD IdDatoLegal int;
//I guess here goes where update every row's IdDatoLegal
//Then when I specify that IdDatoLegal is indexable for future inserts
//and finally when I specify that IdDatoLegal is the new primary key

我希望你能帮助我。

4

1 回答 1

0

Try this:

declare @sql nvarchar(max)

select @sql = 'alter table DatosLegales drop constraint ' + quotename(name)
from sys.indexes 
where object_id = object_id('DatosLegales')
and index_id = 1

exec(@sql)

alter table  DatosLegales
add IdDatoLegal bigint not null identity(1,1) 
constraint pk_DatosLegales primary key clustered

select * from DatosLegales
于 2012-12-14T00:24:38.180 回答