Solved

MS SQL DROP DEFAULT CONSTRAINT

Posted on 2014-12-27
6
292 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 40520982
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
ID: 40521190
@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
ID: 40521335
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 15

Expert Comment

by:Vikas Garg
ID: 40521719
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

717 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