Modify the default value of a field in mssql

Source: Internet
Author: User

1. Obtain the default values of all tables in the database:

The code is as follows: Copy code

Select

T3.name as table name, t1.name as field name, t2.text as default value, t4.name

From syscolumns t1, syscomments t2, sysobjects t3, sysobjects t4

Where t1.cdefault = t2.id and t3.xtype = 'u' and t3.id = t1.id

And t4.xtype = 'd' and t4.id = t2.id;

2. Generate a statement to delete all default values:

The code is as follows: Copy code

Select "alter table" + t3.name + "drop constraint" + t4.name + ";"

From syscolumns t1, syscomments t2, sysobjects t3, sysobjects t4

Where t1.cdefault = t2.id and t3.xtype = 'u' and t3.id = t1.id

And t4.xtype = 'd' and t4.id = t2.id;

3. Generate statements to add all default values in batches:

The code is as follows: Copy code

Select "alter table" + t3.name + "add default (1) for" + t1.name + ";"

From syscolumns t1, syscomments t2, sysobjects t3, sysobjects t4

Where t1.cdefault = t2.id and t3.xtype = 'u' and t3.id = t1.id

And t4.xtype = 'd' and t4.id = t2.id;

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.