Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 .NET Inside SQL Server (2005)
 check if foreign key is in use

Author  Topic 

Jouni79
Starting Member

9 Posts

Posted - 2009-03-10 : 03:35:56
Hello everyone.

I hope I put this in right area. I want to ask your opinion. How to check is foreign key in use .Now when try to delete record witch have reference to other table. now I get The DELETE statement conflicted with the REFERENCE constraint. I want to just check if foreign key is in use.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-10 : 04:20:14
just check the table in which fk is referenced using IF NOT EXISTS

IF NOT EXISTS(SELECT 1 FROM ChildTable WHERE fk= yourvalue)
DELETE m FROM Master m WHERE m.pk=yourvalue


or if you want to do all of deletes in batch then use

DELETE m
FROM Master m
LEFT JOIN Child c
ON c.fk=m.pk
WHERE c.fk IS NULL
Go to Top of Page

Jouni79
Starting Member

9 Posts

Posted - 2009-03-11 : 03:13:32
Thank you.That helped me.
Go to Top of Page
   

- Advertisement -