1

Recently I set value for Default Value or Binding to (newid()) in table design.

Now I want to generate scripts for the changes to execute on another database.

Can anyone please help me to write/generate script for these changes.

Thanks in advance!

Note: I have checked this solution but not helped me coz I want to generate script for all the tables in which I have made changes recently for Default Value or Binding

Community
  • 1
  • 1
Pedram
  • 6,256
  • 10
  • 65
  • 87
  • Possible duplicate of [How to set a default value for an existing column](http://stackoverflow.com/questions/6791675/how-to-set-a-default-value-for-an-existing-column) – pmcilreavy Jan 31 '17 at 09:59

1 Answers1

3

Use the below query to generate the script for default constraints with default value newid()

SELECT 'alter table ' + Quotename(o.name)
       + ' add constraint ' + Quotename(dc.name)
       + ' default ' + dc.definition + ' for '
       + Quotename(c.name)
FROM   sys.default_constraints dc
       INNER JOIN sys.columns c
               ON dc.parent_object_id = c.object_id
                  AND dc.parent_column_id = c.column_id
       INNER JOIN sys.objects o
               ON o.object_id = dc.parent_object_id
WHERE  definition = '(newid())' 

copy paste the result in new database and execute it create the default constraints.

Pரதீப்
  • 91,748
  • 19
  • 131
  • 172