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

I would like to remove all the default values ​​that were set in a particular database, is there a script that I can execute to do this for all tables in the database? Maybe it will take a long time without ... any help would be appreciated!

+3
source share
2 answers

If you are running SQL Server 2005 or later, you can use this script to create another script that will then drop all of your default restrictions:

SELECT 
    'ALTER TABLE dbo.' + OBJECT_NAME(parent_object_id) + 
    ' DROP CONSTRAINT ' + Name
FROM
    sys.default_constraints

This will create a series of operators as output, such as

ALTER TABLE dbo.YourTable DROP CONSTRAINT DF_T_YourColumn

SQL Server Management Studio , !

+4

script, :

SELECT 'alter table ' + object_name(parent_object_id) + 
    ' drop constraint ' + name
FROM sys.objects
WHERE type = 'D'
+3

Source: https://habr.com/ru/post/1722201/


All Articles