Rahul
Rahul

Reputation: 81

What should be the query for setting foreign key to zero in sql server r2?

I am using

SET FOREIGN_KEY_CHECKS=0;

for My sql but i need the same query in Sql Server, I tried this

ALTER TABLE MyTable NOCHECK CONSTRAINT ALL;

but its not working for me somehow. Can anybody tell me how to write

SET FOREIGN_KEY_CHECKS=1;

same query in sql server?

Upvotes: 0

Views: 6478

Answers (1)

krtek
krtek

Reputation: 402

Simply:

ALTER TABLE MyTable
      NOCHECK CONSTRAINT ALL;
GO


INSERT INTO MyTable
...


ALTER TABLE MyTable
    CHECK CONSTRAINT ALL;
GO

You can check then if any keys violate the constraints...

DBCC CHECKCONSTRAINTS('MyTable')
GO

Upvotes: 6

Related Questions