Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
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 blockCheesy Example:
create table t1 (a int primary key, b int);gocreate table t2 (a int foreign key references t1(a));goinsert t1 values(1, 1);insert t1 values(2, 2);goinsert t2 values(1);goBegin TryDelete t1 where a = 1select 1 as successEnd tryBegin Catch select 0 as successEnd catchGOdrop table t2drop table t1