Important alert: (current site time 7/15/2013 7:35:05 PM EDT)
 

VB icon

Toggle Table Relations

Email
Submitted on: 5/29/2002 9:43:53 AM
By: Lewis E. Moten III  
Level: Intermediate
User Rating: By 1 Users
Compatibility: Other
Views: 12348
author picture
(About the author)
 
     Useful for disabling constraints during database replication. When copying data to another database, use this procedure on the target database first. When finnished, run it again to enable the constraints. I used this because I was working on a database with tables referencing themselves. I couldn't add records when previouse records did not exist. This solved the problem.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
--**************************************
-- Name: Toggle Table Relations
-- Description:Useful for disabling constraints during database replication. When copying data to another database, use this procedure on the target database first. When finnished, run it again to enable the constraints. I used this because I was working on a database with tables referencing themselves. I couldn't add records when previouse records did not exist. This solved the problem.
-- By: Lewis E. Moten III
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=481&lngWId=5--for details.--**************************************

CREATE PROCEDURE dbo.sprCheckRelations
(
	@Check BIT = 1
)
AS
	DECLARE @Table VARCHAR(128)
	DECLARE @Constraint VARCHAR(128)
	DECLARE @SQL VARCHAR(1000)
	DECLARE @True BIT
	DECLARE @False BIT
	
	SET @True = 1
	SET @False = 0
	
	-- Grab a list of all relational constriaints in database
	DECLARE Constraints CURSOR FOR 
		SELECT TABLE_NAME, CONSTRAINT_NAME 
		FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
	
	OPEN Constraints
	
	-- Loop through each constraint
	FETCH NEXT FROM Constraints INTO @Table, @Constraint
	
	WHILE @@FETCH_STATUS = 0
	BEGIN
		-- Determine if we are to check constraints
		IF @Check = @True
			SET @SQL = 'ALTER TABLE ' + @Table + ' CHECK CONSTRAINT ' + @Constraint
		ELSE
			SET @SQL = 'ALTER TABLE ' + @Table + ' NOCHECK CONSTRAINT ' + @Constraint
	
		-- Add/Remove constraint
		EXEC(@SQL)
		-- Move to next constraint
		FETCH NEXT FROM Constraints INTO @Table, @Constraint
	END
	
	CLOSE Constraints
	
	DEALLOCATE Constraints


Other 31 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.