﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Discuss content posted by Toby Rogers / Article Discussions / Article Discussions by Author  / Delete constraints - SQL Server 2005 / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Mon, 20 May 2013 02:15:38 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Delete constraints - SQL Server 2005</title><link>http://www.sqlservercentral.com/Forums/Topic401454-446-1.aspx</link><description>Try this:[code]USE MyDBDECLARE @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, nameWHILE 1 = 1BEGIN   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 FIRST FROM cur INTO @sConstraintName, @sTypeDesc, @sTableName   END   CLOSE curENDDEALLOCATE curGO[/code]</description><pubDate>Sun, 12 Jul 2009 01:46:21 GMT</pubDate><dc:creator>Larry Leonard-399461</dc:creator></item><item><title>RE: Delete constraints - SQL Server 2005</title><link>http://www.sqlservercentral.com/Forums/Topic401454-446-1.aspx</link><description>Thank you.  This saved me the trouble of writing something like it.Incidentally, it does not get rid of defaults if you consider those constraints.</description><pubDate>Thu, 24 Jul 2008 16:08:47 GMT</pubDate><dc:creator>timothyawiseman</dc:creator></item><item><title>Delete constraints - SQL Server 2005</title><link>http://www.sqlservercentral.com/Forums/Topic401454-446-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/scripts/Miscellaneous/31937/"&gt;Delete constraints - SQL Server 2005&lt;/A&gt;[/B]</description><pubDate>Sat, 22 Sep 2007 13:05:35 GMT</pubDate><dc:creator>sqldummy79</dc:creator></item></channel></rss>