Codebureau - Matt Simner
(Not) just another .NET Developer
Monday, September 28, 2009
SQL Server Stored Procedure to disable / enable all foreign keys and constraints in a database
I've been doing a bit of batch archiving work, and needed a nice and quick way to disable foreign keys and other constraints without actually removing them. I found a
nice article on disabling all constraints in a database
, and thought I'd just take it one step further by making it into a Stored Procedure, and adding a parameter to toggle whether the constraints are enabled or disabled.
Nice and easy. Here's the script.
USE
[YOURDBNAME]
GO
IF EXISTS
(
SELECT
*
FROM
sys.objects
WHERE object_id = OBJECT_ID
(
N'[dbo].[usp_SetDBTableConstraints]'
)
AND
type
in
(
N'P'
,
N'PC'
))
DROP PROCEDURE
dbo.usp_SetDBTableConstraints
GO
CREATE PROCEDURE
dbo.usp_SetDBTableConstraints(@Enable
BIT
)
AS
/*** DISABLE/ENABLE ALL TABLE CONSTRAINTS ************************
This script will disable all constraints on all tables within the database
that it is run in.
************************************************************/
SET NOCOUNT ON
SET ROWCOUNT
0
DECLARE
@Count
INT
DECLARE
@String
NVARCHAR
(
1000
)
DECLARE
@ConstraintName
VARCHAR
(
128
)
DECLARE
@TableName
VARCHAR
(
128
)
--Find all constraints and their respective tables from the sysobjects table and place into a temp table.
--Primary Key and Unique Constraints via Unique Indexes are not disabled through this command
--You should use the ALTER INDEX...DISABLE command in SQL Server 2005
SELECT
name AS
constraintname,
object_name(parent_obj)
AS
tablename
INTO #
Const_Table
FROM
sysobjects s
where
xtype
in
(
'F'
)
SELECT
@Count
=
Count(*)
FROM #
Const_Table
--Setting the rowcount to one allows for one row from the temp table to be picked off at a time.
--Used as an alternative to a cursor.
SET ROWCOUNT
1
--Loop until all rows in temp table have been processed.
WHILE
@Count >
0
BEGIN
--The rowcount of one ensures that only one tablename and constraint name is picked.
SELECT
@TableName
=
TableName, @ConstraintName
=
ConstraintName
FROM #
Const_Table
--Build execution string to disable constraint.
IF
@Enable
=
1
SET
@String
=
'ALTER TABLE ['
+ @tablename +
'] CHECK CONSTRAINT ['
+ @constraintname +
']'
ELSE
SET
@String
=
'ALTER TABLE ['
+ @tablename +
'] NOCHECK CONSTRAINT ['
+ @constraintname +
']'
--Execute the SQL
EXEC sp_executesql
@string
--Remove this row from the temp table, since it has now been processed.
DELETE FROM #
Const_Table
WHERE
ConstraintName
=
@ConstraintName
and
TableName
=
@TableName
SET
@Count
=
@Count -
1
END
-- Loop
SET ROWCOUNT
0
DROP TABLE #
Const_Table
GO
SQL Server
posted on Monday, September 28, 2009 12:34:45 PM (AUS Eastern Standard Time, UTC+10:00)
Comments [0]
Related posts:
SQL Server - Insert to table with ALL default values
HOW TO Use SQL Server DTS Transform Data Task to export to dynamic Excel spreadsheet file
Using local SQL 2005 Express databases, |DataDirectory| setting and SQLBulkCopy with SemiColon delimited text files
Create SQL Server User and Role Script to execute Stored Procedures Only
Refactoring the inefficient data loop
Search SQL Server Stored Procedures for any text string
Comments are closed.
Navigation
Codebureau Home
Matt Simner - T-Shirts, Software, Design
CodeProject Articles
Geek Casuals T-Shirts
On this page
Archive
<
March 2010
>
Sun
Mon
Tue
Wed
Thu
Fri
Sat
28
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
March, 2010 (1)
February, 2010 (2)
January, 2010 (1)
October, 2009 (2)
September, 2009 (1)
August, 2009 (1)
July, 2009 (1)
June, 2009 (3)
May, 2009 (1)
April, 2009 (3)
March, 2009 (2)
February, 2009 (2)
January, 2009 (3)
December, 2008 (4)
November, 2008 (3)
October, 2008 (4)
September, 2008 (3)
August, 2008 (3)
July, 2008 (16)
June, 2008 (10)
May, 2008 (1)
April, 2008 (6)
March, 2008 (4)
February, 2008 (7)
January, 2008 (10)
December, 2007 (2)
November, 2007 (3)
September, 2007 (3)
August, 2007 (5)
July, 2007 (5)
June, 2007 (4)
April, 2007 (1)
March, 2007 (2)
February, 2007 (2)
January, 2007 (6)
July, 2006 (1)
March, 2006 (1)
July, 2005 (2)
June, 2005 (1)
May, 2005 (3)
February, 2005 (3)
Month View
Categories
.NET Framework
Agile
Articles
ASP.NET
C#
CMS
Creative Design
CRM
dasBlog
Development Process
DotNetNuke
FavPal.NET
InfoPath
IT Musings
JQuery
Miscellaneous
Networking
Off piste
Oracle
Performance
Redbubble
Refactoring
Security
Setup and Deployment
SharePoint
SQL Server
SubVersion
Tools
Unit Testing
Usability
Visual Studio
Web
XML
Blogroll
Brad Abrams
Charlie Poole (NUnit)
Roy Osherove
Scott Hanselman
SQL Authority - Pinal Dave