Solved

MS SQL DROP DEFAULT CONSTRAINT

Posted on 2014-12-27
6
234 Views
Last Modified: 2014-12-29
I noticed, from the object sys.default_constraints, that MS SQL generates individual default constraints for each field set with a default value.
Please, note that I set the default values with one Alter table  table1 add constraint tableconstr1 default ... for ...., and this statement was built using information from information_schema.columns.
Is there a way to drop all  the default constraints generated automatically by MS SQL with one statement like
ALTER table table1 DROP CONSTRAINT tableconstr1 (name previously used in the statement above).

Thanks in advance for your time.
0
Comment
Question by:Omer-Pitou
6 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
Is there a way to drop all  the default constraints generated automatically by MS SQL with one statement
As the syntax for ALTER TABLE shows you can drop as many constraints as you like with one statement.
0
 

Author Comment

by:Omer-Pitou
Comment Utility
@Portlet Paul,
Since  the link that was providing an answer to my question is deleted,  how can I recover it.
0
 
LVL 3

Accepted Solution

by:
Ali HND earned 500 total points
Comment Utility
to drop default constraint that you don't knowing its name:
declare @table_name nvarchar(256)
declare @col_name nvarchar(256)
set @table_name = N’Department’
set @col_name = N’ModifiedDate’

select t.name, c.name, d.name, d.definition
from sys.tables t
    join
    sys.default_constraints d
        on d.parent_object_id = t.object_id
    join
    sys.columns c
        on c.object_id = t.object_id
        and c.column_id = d.parent_column_id
where t.name = @table_name
and c.name = @col_name

Open in new window


you can also refer to this link:
DROP DEFAULT (Transact-SQL)
0
 
LVL 14

Expert Comment

by:Vikas Garg
Comment Utility
Hi,

You try the following

DECLARE @sql NVARCHAR(MAX) = N'';

SELECT @sql += N'
ALTER TABLE ' + QUOTENAME(OBJECT_SCHEMA_NAME(parent_object_id))
    + '.' + QUOTENAME(OBJECT_NAME(parent_object_id)) + 
    ' DROP CONSTRAINT ' + QUOTENAME(name) + ';'
FROM sys.foreign_keys;

--PRINT @sql;
EXEC sp_executesql @sql;

Open in new window


Or can try this as well

http://www.source-code.biz/snippets/mssql/2.htm

Hope this would help
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now