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

avatar image

mattruma
21 1 1 3

(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, 2009 at 10:14 AM

avatar image

Matt Whitfield ♦♦
29.5k 62 66 88

(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

avatar image

Rob Farley
5.8k 16 22 28

(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

SQL Server Central

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

Topics:

x409
x86

asked: Nov 07, 2009 at 09:22 AM

Seen: 2703 times

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

Copyright 2016 Redgate Software. Privacy Policy