x

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, 2009 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 oldest

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

DECLARE @SQL varchar 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, 2009 at 10:14 AM

Matt Whitfield gravatar image

Matt Whitfield ♦♦
29.5k 61 65 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, 2009 at 04:30 AM

Rob Farley gravatar image

Rob Farley
5.7k 16 18 20

(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.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

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

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x346
x69

asked: Nov 07, 2009 at 09:22 AM

Seen: 2285 times

Last Updated: Nov 07, 2009 at 09:22 AM