When modifying the table field type in SQL database, the following error occurs: "One or more objects access this column"

Source: Internet
Author: User

When you use an SQL statement (Format: alter table [tablename] alter column [colname] [newdatatype]) in a SQL database to modify the field type of a table, an error is returned:

"*********************************

The object 'df _ ******* 'depends on the column 'column1 '.
***********************************
Because one or more objects access this column, Alter table ********* failed ."

According to the above prompt, search for the result online and find that the cause of this error is that the field to be modified has the default constraint. solution:

1. Find the name of the constraint for this field in the table (or based on the existing prompt and the object 'df __******')

Declare @ name varchar (50)
Select @ name = B. name from sysobjects B join syscolumns A on B. ID = A. cdefault
Where a. ID = object_id ('tablename ')
And a. Name = 'columname'

2. Delete existing constraints

Exec ('alter table tablename drop constraint' + @ name)

3. Run the script to modify the field type.

Based on the data query, it is found that all operations such as setting the field in the table to not null (not empty) and adding the default value (default value) to the field will add constraints to the field, after these constraints are added, a similar error occurs when you use an SQL script to modify the field type or delete a field. You need to find the existing constraints on the field and Delete the existing constraints, execute the modify/delete script again.

Reference:

1. When modifying the field type, the column classinfo_showtype is dependent, because one or more objects access this column.

2. SQL server: delete columns with default values from a table using SQL statements.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.