[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.2

Delete all contraints in a database

Asked by ksfok in MS SQL Server

Tags: all, drop, constraints, database, contraints

Given this script to delete all contrants in a SQL 2005 database:

-- Make sure we are on the correct db

USE [TD_Backup]
GO
--- Do not show record counts

SET NOCOUNT ON

--- Declare variables to be use in script

DECLARE      @ObjectID      INT
,            @ObjectName VARCHAR(500)
,            @SQL            NVARCHAR(2000)
,            @Count            INT
,            @ObjectType      VARCHAR(1000)

--- Create temp table that is going to be used throughout the script

CREATE TABLE #ObjectTemp (ObjectID INT IDENTITY(1,1) NOT NULL, ObjectName VARCHAR(250), ObjectType VARCHAR(100))
SET @Count = 0

--- First let's drop all the constraints on the tables
------ CAUTION:  Running this part removes all constraints from the database -------

INSERT INTO #ObjectTemp (ObjectName, ObjectType)
SELECT Table_Schema + '.' + Table_Name, Constraint_Name
FROM INFORMATION_SCHEMA.Table_CONSTRAINTS
ORDER BY constraint_type , Table_Name

SELECT @ObjectID = MIN(ObjectID) FROM #ObjectTemp

WHILE @ObjectID IS NOT NULL
BEGIN

SELECT      @ObjectName = ObjectName
,      @ObjectType = ObjectType
FROM #ObjectTemp WHERE ObjectID = @ObjectID

SET @SQL = 'ALTER TABLE ' + @ObjectName + ' DROP CONSTRAINT [' + @ObjectType + ']'

EXECUTE SP_EXECUTESQL @SQL

SELECT @ObjectID = MIN(ObjectID) FROM #ObjectTemp WHERE ObjectID > @ObjectID
SET @ObjectName = NULL
SET @SQL = NULL
SET @COUNT = @Count + 1

END
PRINT CAST(@Count AS VARCHAR(10)) + ' Constraint(s) deleted'ill

Will the above script work for a SQL2000 database?


[+][-]11/04/07 07:53 PM, ID: 20213746Accepted Solution

Your question has an Asker Certified™ answer! ksfok verified that this solution worked for them--which means it will likely work for you, too. Click to view the solution free for 30-days now.

About this solution

Zone: MS SQL Server
Tags: all, drop, constraints, database, contraints
Sign Up Now!
Solution Provided By: aneeshattingal
Participating Experts: 1
Solution Grade: B
 
 
Loading Advertisement...
20100608-EE-VQP-165 - Hierarchy / EE_QW_2_20070628