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
 Transact-SQL (2005)
 System Function to Determine if Parent exists

Author  Topic 

denis_the_thief
Aged Yak Warrior

596 Posts

Posted - 2010-08-09 : 15:40:13
Is there any built-in Function or S.P. that returns true/false depending on wether or not a Child Record exists ie an FK pointing to the record? The input would by the Table and the value of the Primary Key. Basically the function would return wether or not the record could be deleted in the sense it would not violate an FK.

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2010-08-09 : 21:59:23
No, but you could wrap the delete in a try/catch block

Cheesy Example:
create table t1 (a int primary key, b int);
go
create table t2 (a int foreign key references t1(a));
go
insert t1 values(1, 1);
insert t1 values(2, 2);
go

insert t2 values(1);
go

Begin Try
Delete t1 where a = 1
select 1 as success
End try

Begin Catch
select 0 as success
End catch
GO
drop table t2
drop table t1
Go to Top of Page
   

- Advertisement -