How do I drop a column with object dependencies in

2019-02-07 20:13发布

The error message I'm obtaining when trying to drop a column:

The object 'defEmptyString' is dependent on column 'fkKeywordRolleKontakt'.

Msg 5074, Level 16, State 1, Line 43

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

I have already tried to find the default constraints, as described here: SQL Server 2005 drop column with constraints

Unfortunately without any success :( The line returned is:

fkKeywordRolleKontakt 2 814625945 0 defEmptyString

And I cannot remove either of fkKeywordRolleKontakt and defEmptyString.

What is the correct way to get rid of this dependency?

EDIT: Perhaps this is of importance too. The column fkKeywordRolleKontakt is of type udKeyword (nvarchar(50)) with default dbo.defEmptyString.


Edit 2: Solved

I could solve the problem now. I'm sorry, I did not copy the full error message, which was:

Msg 5074, Level 16, State 1, Line 1
The object 'defEmptyString' is dependent on column 'fkKeywordRolleKontakt'.
Msg 5074, Level 16, State 1, Line 1
The object 'FK_tlkpRolleKontakt_tlkpKeyword' is dependent on column 'fkKeywordRolleKontakt'.
Msg 4922, Level 16, State 9, Line 1
ALTER TABLE DROP COLUMN fkKeywordRolleKontakt failed because one or more objects access this column.

I could generate a script to drop the column by right-clicking on the column entry (dbo.tlkpRolleKontakt > Columns > fkKeywordRolleKontakt) (in MSSQL Server Manager), selecting Modify and deleting the column. Then Table Designer > Generate Change Script generated the necessary commands:

ALTER TABLE dbo.tlkpRolleKontakt
    DROP CONSTRAINT FK_tlkpRolleKontakt_tlkpKeyword
EXECUTE sp_unbindefault N'dbo.tlkpRolleKontakt.fkKeywordRolleKontakt'
ALTER TABLE dbo.tlkpRolleKontakt
    DROP COLUMN fkKeywordRolleKontakt

That's it :)

5条回答
一纸荒年 Trace。
2楼-- · 2019-02-07 20:22

I could solve the problem now. I'm sorry, I did not copy the full error message, which was:

Msg 5074, Level 16, State 1, Line 1
The object 'defEmptyString' is dependent on column 'fkKeywordRolleKontakt'.

Msg 5074, Level 16, State 1, Line 1
The object 'FK_tlkpRolleKontakt_tlkpKeyword' is dependent on column 'fkKeywordRolleKontakt'.
Msg 4922, Level 16, State 9, Line 1 ALTER TABLE DROP COLUMN fkKeywordRolleKontakt failed because one or more objects access this column.

I could generate a script to drop the column by right-clicking on the column entry (dbo.tlkpRolleKontakt > Columns > fkKeywordRolleKontakt) (in MSSQL Server Manager), selecting Modify and deleting the column. Then Table Designer > Generate Change Script generated the necessary commands:

ALTER TABLE dbo.tlkpRolleKontakt
    DROP CONSTRAINT FK_tlkpRolleKontakt_tlkpKeyword
EXECUTE sp_unbindefault N'dbo.tlkpRolleKontakt.fkKeywordRolleKontakt'
ALTER TABLE dbo.tlkpRolleKontakt
    DROP COLUMN fkKeywordRolleKontakt
查看更多
Emotional °昔
3楼-- · 2019-02-07 20:26

drop the constraint which is dependent on that column with

ALTER TABLE TableName DROP CONSTRAINT dependent_constraint

Then Drop Column:

ALTER TABLE TABLE_NAME DROP COLUMN COLUMN_NAME

dependent_constraint : this constraint is shown in the error when we try to delete dependent column.

Example: trying to drop some column IsDeleted2

Error

The object 'DF__Employees__IsDel__15502E78' is dependent on column 'IsDeleted2'.

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

Error clearly states that we need to delete DF__Employees__IsDel__15502E78 constraint

ALTER TABLE Employess 
DROP CONSTRAINT DF__Employees__IsDel__15502E78;

Drop Column: ALTER TABLE Employess DROP COLUMN IsDelted2

查看更多
Fickle 薄情
4楼-- · 2019-02-07 20:26

use this script to cancel the checking of constraint :

ALTER TABLE  @tablename  NOCHECK CONSTRAINT  @constraintname 
查看更多
【Aperson】
5楼-- · 2019-02-07 20:30

Did you try first:

ALTER TABLE <tablename> DROP CONSTRAINT defEmptyString;

?

查看更多
趁早两清
6楼-- · 2019-02-07 20:44

I ran into a simpler solution.

  1. DELETE the data of that column.
  2. Once the column has no value inside it do -

ALTER TABLE <table_name> DROP COLUMN <column_name>

This way the column is easily dropped.

P.S - This is a headache if you have like extreme amounts of data in the column.

查看更多
登录 后发表回答