T-SQL:将列更改为不再是标识并删除约束

时间:2016-08-25 09:30:58

标签: sql tsql

我试图更改ID table中的列[file].Item,以便它不再是表格的identity。我还想删除指向列的所有foreign key约束。

我的问题是,我想用脚本执行此操作,该脚本将删除对列的所有引用,然后删除它,以便我可以重新制作它。

到目前为止我所拥有的是:

exec sp_MSforeachtable 'alter table ? nocheck constraint all'

alter table [file].Item
drop column ID

alter table [file].Item
add ID integer not null

exec sp_MSforeachtable 'alter table ? with check check constraint all'

我得到的错误是:

ALTER TABLE DROP COLUMN ID failed because one or more objects access this column.


修改

在查看对我的解决方案后,我尝试了以下代码:

CREATE TABLE test(
     [ID] [int] NOT NULL,
     [Parent_ID] [int] NULL,
     [Company_ID] [int] NOT NULL,
     [ItemType_ID] [int] NOT NULL,
     [ItemCode] [varchar](20) NOT NULL,
     [ItemNumber] [varchar](20) NOT NULL,
     [Type] [char](1) NOT NULL,
     [ItemDetails] [nvarchar](50) NULL,
     [Condition] [varchar](10) NULL,
     [DateTimeCreated] [datetime] NOT NULL,
     [DateTimeModified] [datetime] NOT NULL,
     [CreatorUser_ID] [int] NOT NULL,
     [LastModifierUser_ID] [int] NOT NULL,
     [Status] [tinyint] NOT NULL,
     [IsUploadToGlobal] [bit] NOT NULL,
     [Quantity] [numeric](18, 6) NOT NULL,
     [TotalQuantity] [numeric](18, 6) NOT NULL,
     [OnHand] [numeric](18, 6) NOT NULL,
     [Sold] [numeric](18, 6) NOT NULL,
     [OnOrder] [numeric](18, 6) NOT NULL,
     [MinQuantity] [numeric](18, 6) NULL,
     [MaxQuantity] [numeric](18, 6) NULL,
     [Name] [nvarchar](200) NULL,
     [LastInvoice_ID] [int] NULL,
     [OnPurchaseOrder] [numeric](18, 6) NOT NULL,
     [rv] [timestamp] NOT NULL,
     [ExternalCompanyID] [int] NULL,
     [ExternalCompanyName] [nvarchar](50) NULL,
     [ExternalItemID] [int] NULL,
     [LastOrder_ID] [int] NULL,
     [PreviousStatus] [tinyint] NULL,
     [DateTimeStatusChanged] [datetime] NULL,
     [Trashed] [numeric](18, 6) NOT NULL)


     ALTER TABLE test ADD  CONSTRAINT [DF_Item_DateTimeCreated]  DEFAULT (getutcdate()) FOR [DateTimeCreated]


     ALTER TABLE test ADD  CONSTRAINT [DF_Item_DateTimeModified]  DEFAULT (getutcdate()) FOR [DateTimeModified]


    ALTER TABLE test ADD  CONSTRAINT [DF_Item_Status]  DEFAULT ((0)) FOR [Status]


    ALTER TABLE test ADD  CONSTRAINT [DF_Item_IsUploadToGlobal]  DEFAULT ((1)) FOR [IsUploadToGlobal]


    ALTER TABLE test ADD  CONSTRAINT [DF_Item_Quantity]  DEFAULT ((0)) FOR [Quantity]


    ALTER TABLE test ADD  CONSTRAINT [DF_Item_TotalQuantity]  DEFAULT ((0)) FOR [TotalQuantity]


    ALTER TABLE test ADD  CONSTRAINT [DF_Item_OnHand]  DEFAULT ((0)) FOR [OnHand]


    ALTER TABLE test ADD  CONSTRAINT [DF_Item_Sold]  DEFAULT ((0)) FOR [Sold]


    ALTER TABLE test ADD  CONSTRAINT [DF_Item_OnOrder]  DEFAULT ((0)) FOR [OnOrder]


    ALTER TABLE test ADD  CONSTRAINT [DF_Item_OnPurchaseOrder]  DEFAULT ((0)) FOR [OnPurchaseOrder]


    ALTER TABLE test ADD  CONSTRAINT [DF_Item_Trashed]  DEFAULT ((0)) FOR [Trashed]

    ALTER TABLE test  WITH CHECK ADD  CONSTRAINT [FK_Item_ItemType] FOREIGN KEY([ItemType_ID])
    REFERENCES [ref].[ItemType] ([ID])


    ALTER TABLE test CHECK CONSTRAINT [FK_Item_ItemType]


    ALTER TABLE test  WITH CHECK ADD  CONSTRAINT [FK_Item_User] FOREIGN KEY([CreatorUser_ID])
    REFERENCES [system].[User] ([ID])


    ALTER TABLE test CHECK CONSTRAINT [FK_Item_User]


    ALTER TABLE test  WITH CHECK ADD  CONSTRAINT [FK_Item_User1] FOREIGN KEY([LastModifierUser_ID])
    REFERENCES [system].[User] ([ID])


    ALTER TABLE test CHECK CONSTRAINT [FK_Item_User1]


    ALTER TABLE test  WITH CHECK ADD  CONSTRAINT [CK_Item_ActivePartNotSold] CHECK  (([Type]<>'P' OR [Type]='P' AND ([Status]>(1) OR [Status]<=(1) AND [Sold]=(0))))


    ALTER TABLE test CHECK CONSTRAINT [CK_Item_ActivePartNotSold]


    ALTER TABLE test  WITH CHECK ADD  CONSTRAINT [CK_Item_Available] CHECK  ((([OnHand]-[OnOrder])=[Quantity]))


    ALTER TABLE test CHECK CONSTRAINT [CK_Item_Available]


    ALTER TABLE test  WITH CHECK ADD  CONSTRAINT [CK_Item_OnOrder] CHECK  (([OnOrder]>=(0) AND [OnOrder]<=[TotalQuantity]))


    ALTER TABLE test CHECK CONSTRAINT [CK_Item_OnOrder]


    ALTER TABLE test  WITH CHECK ADD  CONSTRAINT [CK_Item_OnPurchaseOrder] CHECK  (([OnPurchaseOrder]>=(0)))


    ALTER TABLE test CHECK CONSTRAINT [CK_Item_OnPurchaseOrder]


    ALTER TABLE test  WITH CHECK ADD  CONSTRAINT [CK_Item_Quantity] CHECK  (([Quantity]>=(0) AND [Quantity]<=[TotalQuantity]))


    ALTER TABLE test CHECK CONSTRAINT [CK_Item_Quantity]


    ALTER TABLE test WITH CHECK ADD  CONSTRAINT [CK_Item_ReservedParts] CHECK  (([Type]<>'P' OR [Type]='P' AND ([Status]<>(100) OR [Status]=(100) AND [OnOrder]=[TotalQuantity])))


    ALTER TABLE test CHECK CONSTRAINT [CK_Item_ReservedParts]


    ALTER TABLE test  WITH CHECK ADD  CONSTRAINT [CK_Item_SoldParts] CHECK  (([Type]<>'P' OR [Type]='P' AND ([Status]<>(150) OR [Status]=(150) AND [Sold]=[TotalQuantity])))


    ALTER TABLE test CHECK CONSTRAINT [CK_Item_SoldParts]


    ALTER TABLE test  WITH CHECK ADD  CONSTRAINT [CK_Item_ToralQuantity] CHECK  (([TotalQuantity]=((([Quantity]+[Sold])+[OnOrder])+[Trashed])))


    ALTER TABLE test CHECK CONSTRAINT [CK_Item_ToralQuantity]


    ALTER TABLE test  WITH CHECK ADD  CONSTRAINT [CK_Item_Trashed] CHECK  (([Trashed]>=(0) AND [Trashed]<=[TotalQuantity]))


    ALTER TABLE test CHECK CONSTRAINT [CK_Item_Trashed]

    ALTER TABLE test  WITH CHECK ADD  CONSTRAINT [CK_Item_TrashedParts] CHECK  (([Type]<>'P' OR [Type]='P' AND ([Status]<>(220) OR [Status]=(220) AND [Trashed]=[TotalQuantity])))


    ALTER TABLE test CHECK CONSTRAINT [CK_Item_TrashedParts]


    ALTER TABLE test  WITH CHECK ADD  CONSTRAINT [CK_Item_Type] CHECK  (([Type]='C' AND [Parent_ID] IS NULL OR [Type]='P' OR [Type]='N'))


    ALTER TABLE test CHECK CONSTRAINT [CK_Item_Type]


     alter table [file].Item switch to test

     select * from test

     drop table [file].Item

     exec sp_rename 'test', '[file].Item'

     select * from [file].Item

我现在得到的错误是:

ALTER TABLE SWITCH statement failed. The table 'file.Item' has clustered index 'PK_Item' while the table 'test' does not have clustered index.

1 个答案:

答案 0 :(得分:0)

禁用表格上的约束:

  ALTER TABLE YourTable NOCHECK CONSTRAINT ALL

重新启用表格上的约束:

 ALTER TABLE YourTable WITH CHECK CHECK CONSTRAINT ALL

禁用所有表的约束:

EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all"

为所有表重新启用约束:

EXEC sp_msforeachtable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all"