Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3396194
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:22:43+00:00 2026-05-18T04:22:43+00:00

I need to drop all the default constraints in our SQL Server 2008 database.

  • 0

I need to drop all the default constraints in our SQL Server 2008 database. Has anyone written a T-SQL script to do this? Note, I just need to drop default constraints, not any other type of constraint.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T04:22:44+00:00Added an answer on May 18, 2026 at 4:22 am

    I post the code, because I hate sites that make you register to read the answer. This code is not mine…backup your database first.

    from http://www.sqlservercentral.com/Forums/Topic401454-446-1.aspx

    USE MyDB
    
    DECLARE @sSql            NVARCHAR(4000) SET @sSql = ''
    DECLARE @sTypeDesc       sysname        SET @sTypeDesc = ''
    DECLARE @sTableName      sysname        SET @sTableName = ''
    DECLARE @sConstraintName sysname        SET @sConstraintName = ''
    
    DECLARE cur CURSOR DYNAMIC FOR
       SELECT name,
              type_desc,
              OBJECT_NAME(parent_object_id)
         FROM sys.objects
        WHERE is_ms_shipped = 0
          AND type_desc LIKE '%_CONSTRAINT'
        ORDER BY type_desc, name
    
    WHILE 1 = 1
    BEGIN
       OPEN cur   
       FETCH FIRST FROM cur INTO @sConstraintName, @sTypeDesc, @sTableName
    
       IF @@FETCH_STATUS != 0
       BEGIN
          CLOSE cur
          BREAK
       END
    
       WHILE @@FETCH_STATUS = 0
       BEGIN
          BEGIN TRY
             SET @sSql = 'ALTER TABLE ' + QUOTENAME(@sTableName) + ' DROP CONSTRAINT ' + QUOTENAME(@sConstraintName)
             RAISERROR('%s', 10, 1, @sSql) WITH NOWAIT   
             EXEC sp_executesql @sSql
          END TRY
          BEGIN CATCH
             DECLARE @ENumber      INT            SET @ENumber      = ISNULL(ERROR_NUMBER(),          -1)
             DECLARE @ESeverity    INT            SET @ESeverity    = ISNULL(ERROR_SEVERITY(),        -1)
             DECLARE @EState       INT            SET @EState       = ISNULL(ERROR_STATE(),            0) IF @EState = 0 SET @EState = 42
             DECLARE @EProcedure   NVARCHAR(126)  SET @EProcedure   = ISNULL(ERROR_PROCEDURE(), N'{N/A}')
             DECLARE @ELine        INT            SET @ELine        = ISNULL(ERROR_LINE(),            -1)
             DECLARE @EMessageRecv NVARCHAR(2048) SET @EMessageRecv = ISNULL(ERROR_MESSAGE(),        N'')
             DECLARE @EMessageSent NVARCHAR(440)  SET @EMessageSent = N''
    
             IF ERROR_PROCEDURE() IS NOT NULL   SET @EMessageSent = N'Error %d, Level %d, State %d, Procedure %s, Line %d, Message: '
             SET @EMessageSent = @EMessageSent + ERROR_MESSAGE()
             RAISERROR(@EMessageSent, 10 /* @ESeverity */ , @EState, @ENumber, @ESeverity, @EState, @EProcedure, @ELine) WITH LOG
          END CATCH
    
          FETCH NEXT FROM cur INTO @sConstraintName, @sTypeDesc, @sTableName
       END
    
       CLOSE cur
    END
    
    DEALLOCATE cur
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to drop a user in a SQL Server script but I'll need
I need to delete many rows from sql server 2008 database, it must be
If you drop a table in SQL Server that has Keys, Indexes, Contraints, etc
Im runing mySQL in a server where i need to drop tons of databases
I am modifying a SQL table through C# code and I need to drop
I need to display an error message on rejecting a drop in my application.
I need to take an existing winforms application and drop into an event tracing
In a drop down list, I need to add spaces in front of the
Folks, I need a solution that allows drag-and-drop cell over the table. The cells
I'm using a MS SQL Server db and use plenty of views (for use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.