Mic*_*ahl 2 sql azure azure-sql-database
我有一个包含标识列的表但我不能删除标识属性.
有没有办法禁用它?或者是一种在没有身份属性的情况下复制整个表的方法?
请注意,如果列由聚簇索引引用,则可能无法删除该列,并且您无法删除表的所有聚簇索引,因为SqlAzure表必须始终具有聚簇索引.
这意味着您可能必须跳过以下环节(至少对于您的最后一个聚簇索引,这可能是您的主键):
这大致如下:
-- Rename clustered index
EXECUTE sp_rename N'PK_My_Current_PK', N'PK_My_Current_PK_OLD', 'OBJECT'
-- If you have any FK constraints on the table, then drop them
ALTER TABLE dbo.MyTable DROP CONSTRAINT FK_My_Foreign_Key
-- Create the new version of your table - because this is SQLAzure it must have a clustered index
CREATE TABLE dbo.tmp_MyTable (
MyID int NOT NULL,
CONSTRAINT PK_My_Current_PK PRIMARY KEY CLUSTERED (MyID)
)
-- Copy the data into the temp table from the old table
INSERT INTO dbo.tmp_MyTable (MyID)
SELECT MyID FROM dbo.MyTable
-- Drop the old table
DROP TABLE dbo.MyTable
-- Rename the new table
EXECUTE sp_rename N'tmp_MyTable', N'MyTable', 'OBJECT'
-- Recreate any foreign key constraints
ALTER TABLE dbo.MyTable WITH CHECK ADD FK_My_Foreign_Key FOREIGN KEY (MyID)
REFERENCES dbo.MyForeignTable (MyID)
Run Code Online (Sandbox Code Playgroud)
希望有所帮助
一个
编辑:正如@PhilBolduc所指出的,SqlAzure表需要聚簇索引,而不是主键.我相应地修改了上面的术语 - 答案的原则仍然存在.