22

In SQL Server 2005, can I issue an SQL query to list all FK constraints on tables within the DB, and show the delete rule? (ie nothing, cascade, set null, or set default)

The output I'm looking for is something akin to:

FK_NAME                  ON_DELETE
==================================
FK_LINEITEM_STATEMENT    CASCADE
FK_ACCOUNTREP_CLIENT     NOTHING
Synesso
  • 37,610
  • 35
  • 136
  • 207

3 Answers3

44

You can try this:

SELECT name, delete_referential_action_desc
FROM sys.foreign_keys
bobs
  • 21,844
  • 12
  • 67
  • 78
  • 1
    Thank you. I was doing things the MS way and double-clicking my way to RSI before you provided this little nugget. – Synesso Oct 06 '10 at 23:14
10

Little late to the game here, but you might also try this:

select * from INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
Undo
  • 25,519
  • 37
  • 106
  • 129
Brian
  • 1,383
  • 3
  • 16
  • 30
1

You can use also expression in the WHERE block:

objectproperty(object_id('FK_your_constraint_name'), 'CnstIsDeleteCascade')

or

objectproperty(your_constraint_object_id, 'CnstIsDeleteCascade')
Undo
  • 25,519
  • 37
  • 106
  • 129
Alexey
  • 11
  • 1