0

Code:

ALTER TABLE tblUser
DROP COLUMN Mobile

Error:

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

This column had values in Table. How can I delete all objects that access this column?

How can I DROP COLUMN with values?

how can do it with code? How can I delete all constraints in column automatically?

5
  • 1
    First check what use it (VIEW WITH SCHEMABINDING, CONSTRAINTS, INDEXES, COMPUTED COLUMNS...) You cannot just drop column when something else depends on it Commented Sep 13, 2015 at 10:54
  • See table definition for indexes/computed columns and constraints or check in Object Explorer in SSMS Commented Sep 13, 2015 at 10:57
  • Seriously, you do DDL on DB and don't know how to check it? Commented Sep 13, 2015 at 10:59
  • how can do it with code? how can Delete all constraints in column Automatic؟ Commented Sep 13, 2015 at 11:10
  • 1
    You need to know what those constraints are and what their names are in order to drop them; there's nothing in SQL Server to say DROP ALL CONSTRAINTS and just do it. Commented Sep 13, 2015 at 11:55

4 Answers 4

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

Your column won't be deleted. Because one column or multiple columns are getting reference from this column that you want to delete.

So first, you will have to find in which table your column is being referenced by below query.

SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME = 'TABLENAME'

It will show you all constraints of all tables of your current database. You need to find it and remove the constraint. After that your column will be deleted successfully because there is no reference of your column in any table.

To remove constraint from column - use below query

alter table tablename
drop constraint constraintid
Sign up to request clarification or add additional context in comments.

Comments

1

SQL Search is a great tool. I will search for your all the objects which are using the targeted object.

You can easily find where your column is being used, then simply you can modify or drop that objects too.

Comments

1

Use below query to find the constraints name for particular tablename

  SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME = 'TABLENAME'

Noe you can see the constraints name under constraint_name column, drop all constraint using below syntax

  ALTER TABLE TABLENAME DROP CONSTRAINT CONSTRATINTSNAME 

After that you can use below statement to drop the column

ALTER TABLE TABLENAME DROP COLUMN COLUMNNAME

Comments

0

You need to know what those constraints are and what their names are in order to drop them; there's nothing in SQL Server to say DROP ALL CONSTRAINTS and just do it. – marc_s yesterday

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.