Dropping Constraints

How do I temporariliy suspend constraints on a table or tables when I'm trying to perform an operation, say, a DELETE?
LVL 1
NigelRocksAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RiteshShahCommented:
for desabling constraint for all table and enable it back after delete


EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
EXEC sp_MSForEachTable 'DELETE FROM ?'
EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RiteshShahCommented:
if you want it for just one table than do it like

ALTER TABLE employee NOCHECK CONSTRAINT ALL
DELETE FROM employee
ALTER TABLE employee CHECK CONSTRAINT ALL
0
NigelRocksAuthor Commented:
Did I award points too soon?  This syntax
ALTER TABLE HRAQuestion NOCHECK CONSTRAINT ALL
DELETE FROM HRAQuestion
ALTER TABLE HRAQuestion CHECK CONSTRAINT ALL
Still produces this error:
 
Msg 547, Level 16, State 0, Line 2
The DELETE statement conflicted with the REFERENCE constraint "HRAQuestion_HRAGroupQuestionsParent". The conflict occurred in database "CCS", table "dbo.HRAGroupQuestions", column 'HragqParentqID'.
The statement has been terminated.
 
What am I missing?
0
RiteshShahCommented:
well, you talked about constraint but this seems the case of foreign key. can you please show me the structure of your table, may be "Create Table" script
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.