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
Solved

MS SQL DROP DEFAULT CONSTRAINT

Posted on 2014-12-27
6
266 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
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
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.

839 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