Saturday, February 25, 2012

Programatically finding if a row has a foreign key constraint

Hello,

Does anyone know a way from within a .net application to find out if a row has a foreign key constraint before updating it? Or possibly in a SQL Stored Procedure if necessary.

What I am trying to do is basicly simulate a delete of a row in a SQL 2005 database from a .net application. Instead of actually deleting the row I want to set a bool column as deleted. The problem is that I can't orphan records that have a relationship to the one deleted.

This is normaly taken care of automaticly with refrential integrity working properly, but not actually calling delete comands I can't seem to find that out without writing a lot of code to check manually in other tables for that key.

Any help would be appreciated greatly,

Thanks,

Patrick

The following statement will bring you the references tables:

Select TC2.Table_Name from INFORMATION_SCHEMA.Table_constraints TC

INNER JOIN INFORMATION_SCHEMA.Referential_constraints RC

ON TC.CONSTRAINT_NAME = RC.UNIQUE_CONSTRAINT_NAME

INNER JOIN INFORMATION_SCHEMA.Table_constraints TC2

ON TC2.CONSTRAINT_NAME = RC.Constraint_NAME

WHERE TC.TABLE_NAME ='Category' AND TC.CONSTRAINT_TYPE = 'PRIMARY KEY'

HTH, Jens SUessmeyer.

http://www.sqlserver2005.de

|||Thanks. That worked

No comments:

Post a Comment