If you are trying to dynamically sort this at runtime to show the user or determine before attempting to delete, and you are not sure of a possible conflict, you can use sys tables and some dynamic sql to sort it.
- Use
sys.objects to find your table name and get id_id. - Use
sys.foreign_keys to find tables that reference your table. - Use sys.
foreign_key_columns to get the exact column numbers. - Use
sys.columns to get the corresponding column names. - Create dynamic SQL to look up table and column names and find offensive rows using values ββfrom the original row that cannot be deleted.
- List the returned tables in
sys.objects . Optionally list the number of rows. - It is not necessary to use dynamic SQL to create other statements (for example, to delete offensive records - this may be required recursively - take care of this - you can lose a lot of data! )
source share