Avatar of Omer-Pitou
Omer-Pitou

asked on 

MS SQL DROP DEFAULT CONSTRAINT

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.
Microsoft SQL ServerMicrosoft SQL Server 2008

Avatar of undefined
Last Comment
Vikas Garg
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

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.
Avatar of Omer-Pitou
Omer-Pitou

ASKER

@Portlet Paul,
Since  the link that was providing an answer to my question is deleted,  how can I recover it.
ASKER CERTIFIED SOLUTION
Avatar of Ali HND
Ali HND
Flag of Iran, Islamic Republic of image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Vikas Garg
Vikas Garg
Flag of India image

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
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo