2016-08-25 54 views
0

我試圖改變table[file].Item中的列ID,以便它不再是表的identity。我也想要刪除指向該列的所有約束條件。T-SQL:改變列不再是身份和刪除約束

然而,我的問題是,我想用腳本來完成這項工作,它將刪除對列的所有引用,然後刪除它,以便我可以重新制作它。

我到目前爲止是這樣的:

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. 


編輯

尋找到解決方案sugested給我後,我試過以下代碼:

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

嘗試運行'EXEC sp_MSforeachtable「改變表? nocheck約束all''然後運行drop column –

+1

也'alter table [file] .Item add ID integer not null'這是不可能的數據存在於表 –

+1

您可能能夠做一些非常類似於[這個答案](http://stackoverflow.com/a/1730868/15498)(但相反)在不丟棄列的情況下脫除標識屬性。 –

回答

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" 
+0

我已經更新了我的問題 –