Ddl SQL scripts that are constrained by the default values of all table fields in MSSQL dB in one breath

Source: Internet
Author: User

Ms SQL Server
Enterprise Manager EM:
You cannot generate an SQL script for all tables in a database.Only generate "Default Value constraints"Related ddl SQL scripts,
Instead of creating a table.
Query analyzer QA:
Ddl SQL scripts related to "Default constraints" can be generated separately,
HoweverCannot be obtained in one breathAll Tables"Default Value constraints"Related ddl SQL scripts.

Do it yourself:
Two methods:
1. Pure SQL multi-Table self join implementation

Select   ' Alter table [ '   + B. Name +   ' ] Add constraint [ '   + A. Name +   ' ] Default '   + D. Text   +   ' For [ '   + C. Name +   ' ] '   As   [ Adddefaconconstraint ]
, ' Alter table [ '   + B. Name +   ' ] Drop constraint [ '   + A. Name +   ' ] '   As   [ Dropdefaconconstraint ]
From Sysobjects
Left   Join Sysobjects B On A. parent_obj = B. ID
Left   Join Syscolumns C On A. parent_obj = C. ID And A.info = C. colid
Left   Join Syscomments d On A. ID = D. id
Where A. xtype =   ' D '
Order   By B. Name

2. Use the MS T-SQL function to make the explicit self join less than twiceCodeConcise

Select   ' Alter table [ '   +   Object_name (A. parent_obj) +   ' ] Add constraint [ '   +   Object_name (A. ID) +   ' ] Default '   + D. Text   +   ' For [ '   +   Col_name (A. parent_obj, a.info) +   ' ] '   As   [ Adddefaconconstraint ]
, ' Alter table [ '   +   Object_name (A. parent_obj) +   ' ] Drop constraint [ '   +   Object_name (A. ID) +   ' ] '   As   [ Dropdefaconconstraint ]
From Sysobjects
Left   Join Syscomments d On A. ID = D. id
Where A. xtype =   ' D '
Order   By   Object_name (A. parent_obj)

 

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.