[SQL Server] SQL statements Delete columns with default values in a table.

Source: Internet
Author: User
If the default value of the column is set when a column is added dynamically, an error is reported when the alter table tablename drop column columnname statement is used. For example,

"Message 5074, level 16, status 1, 1st rows
The object 'df _ tb_salarypar _ AA _ 7db89c09 'depends on the column 'A '.
Message 4922, level 16, status 9, 1st rows
Alter table drop column AA failed because one or more objects access this column ."

The default constraint exists.

What we need to do now:

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

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 the constraint and delete the column.

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

 

 

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.