[SQL Server] Delete columns with default values in the SQL statement table.

Source: Internet
Author: User

If the default value of a column is set when a table column is added dynamically,

If you use the alter table tablename drop column columnname statement, an error is returned,

For example:

"Message 5074, level 16, status 1, 1st row object 'df _ tb_salarypar _ AA _ 7db89c09 'depends on column 'A '.

Alter table drop column AA failed because one or more objects access this column in message 4922, level 16, status 9, and 1st ."

The default constraint exists.

What we need to do now:

1. Find the constraint name for this column in the table.

Declare@ NameVarchar (50) -- Name of the constraint corresponding to the column

Select @ name = B. name from sysobjects B join syscolumns A on B. ID = A. cdefault

Where a. ID = object_id ('Tablename') -- Table name

And a. Name ='Columname'-- Column name

2. Delete the constraint and delete the column.

Exec ('alter tableTablenameDrop constraint '+@ Name)

Exec ('alter tableTablenameDrop ColumnColumname')

Related Article

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.