x
login about faq Site discussion (meta-askssc)

Is there an easy way to remove all default values for a SQL database?

I'd like to remove all default values that have been setup in a particular database, is there a script that I can run to do this for all tables in database? Could be a bit time consuming without ... any help would be appreciated!

more ▼

asked Nov 07 '09 at 09:22 AM in Default

mattruma gravatar image

mattruma
21 1 1 1

(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

Assuming you are on 2005+, and all your defaults are default constraints, and not bound defaults:

DECLARE @SQL [varchar](MAX)
SET @SQL = ''
SELECT @sql = @sql + 'ALTER TABLE [' + SCHEMA_NAME([schema_id]) + '].[' + OBJECT_NAME([parent_object_id]) + '] DROP CONSTRAINT [' + [name] + '];
'  FROM [sys].[default_constraints]
EXEC (@SQL)
more ▼

answered Nov 07 '09 at 10:14 AM

Matt Whitfield gravatar image

Matt Whitfield ♦♦
29.2k 56 63 87

(comments are locked)
10|1200 characters needed characters left

You can also use PowerShell: This script (running at the database level) will script and drop all default constraints.

PS SQLSERVER:\sql\localhost\default\Databases\YourDatabase> dir Tables\*\Columns\*\DefaultConstraint | % {$_.Script(); $_.Drop()}
more ▼

answered Nov 09 '09 at 04:30 AM

Rob Farley gravatar image

Rob Farley
5.7k 14 17 19

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x275
x60

asked: Nov 07 '09 at 09:22 AM

Seen: 1426 times

Last Updated: Nov 07 '09 at 09:22 AM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.