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)