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

前端 未结 7 1556
[愿得一人]
[愿得一人] 2020-12-05 22:26

I am trying to do this:

ALTER TABLE CompanyTransactions DROP COLUMN Created

But I get this:

Msg 5074, Level 16, Stat

相关标签:
7条回答
  • 2020-12-05 22:54

    I had the same problem and this was the script that worked for me with a table with a two part name separated by a period ".".

    USE [DATABASENAME] GO ALTER TABLE [TableNamePart1].[TableNamePart2] DROP CONSTRAINT [DF__ TableNamePart1D__ColumnName__5AEE82B9] GO ALTER TABLE [TableNamePart1].[ TableNamePart1] DROP COLUMN [ColumnName] GO

    0 讨论(0)
  • 2020-12-05 22:55

    You must remove the constraints from the column before removing the column. The name you are referencing is a default constraint.

    e.g.

    alter table CompanyTransactions drop constraint [df__CompanyTr__Creat__0cdae408];
    alter table CompanyTransactions drop column [Created];
    
    0 讨论(0)
  • 2020-12-05 23:01

    You need to do a few things:

    1. You first need to check if the constrain exits in the information schema
    2. then you need to query by joining the sys.default_constraints and sys.columns if the columns and default_constraints have the same object ids
    3. When you join in step 2, you would get the constraint name from default_constraints. You drop that constraint. Here is an example of one such drops I did.
    -- 1. Remove constraint and drop column
    IF EXISTS(SELECT *
              FROM INFORMATION_SCHEMA.COLUMNS
              WHERE TABLE_NAME = N'TABLE_NAME'
                AND COLUMN_NAME = N'LOWER_LIMIT')
       BEGIN
        DECLARE @sql NVARCHAR(MAX)
        WHILE 1=1
            BEGIN
                SELECT TOP 1 @sql = N'alter table [TABLE_NAME] drop constraint ['+dc.name+N']'
                FROM sys.default_constraints dc
                JOIN sys.columns c
                ON c.default_object_id = dc.object_id
                WHERE dc.parent_object_id = OBJECT_ID('[TABLE_NAME]') AND c.name = N'LOWER_LIMIT'
                IF @@ROWCOUNT = 0
                    BEGIN
                        PRINT 'DELETED Constraint on column LOWER_LIMIT'
                        BREAK
                    END
            EXEC (@sql)
        END;
        ALTER TABLE TABLE_NAME DROP COLUMN LOWER_LIMIT;
        PRINT 'DELETED column LOWER_LIMIT'
       END
    ELSE
       PRINT 'Column LOWER_LIMIT does not exist'
    GO
    
    0 讨论(0)
  • 2020-12-05 23:04

    As already written in answers you need to drop constraints (created automatically by sql) related to all columns that you are trying to delete.

    Perform followings steps to do the needful.

    1. Get Name of all Constraints using sp_helpconstraint which is a system stored procedure utility - execute following exec sp_helpconstraint '<your table name>'
    2. Once you get the name of the constraint then copy that constraint name and execute next statement i.e alter table <your_table_name> drop constraint <constraint_name_that_you_copied_in_1> (It'll be something like this only or similar format)
    3. Once you delete the constraint then you can delete 1 or more columns by using conventional method i.e Alter table <YourTableName> Drop column column1, column2 etc
    0 讨论(0)
  • 2020-12-05 23:06

    The @SqlZim's answer is correct but just to explain why this possibly have happened. I've had similar issue and this was caused by very innocent thing: adding default value to a column

    ALTER TABLE MySchema.MyTable ADD 
      MyColumn int DEFAULT NULL;
    

    But in the realm of MS SQL Server a default value on a colum is a CONSTRAINT. And like every constraint it has an identifier. And you cannot drop a column if it is used in a CONSTRAINT.

    So what you can actually do avoid this kind of problems is always give your default constraints a explicit name, for example:

    ALTER TABLE MySchema.MyTable ADD 
      MyColumn int NULL,
      CONSTRAINT DF_MyTable_MyColumn DEFAULT NULL FOR MyColumn;
    

    You'll still have to drop the constraint before dropping the column, but you will at least know its name up front.

    0 讨论(0)
  • 2020-12-05 23:10

    In addition to accepted answer, if you're using Entity Migrations for updating database, you should add this line at the beggining of the Up() function in your migration file:

    Sql("alter table dbo.CompanyTransactions drop constraint [df__CompanyTr__Creat__0cdae408];");
    

    You can find the constraint name in the error at nuget packet manager console which starts with FK_dbo.

    0 讨论(0)
提交回复
热议问题