解决方法
此脚本将显示具有引用要删除的行的行的所有表:
declare @RowId int = 1 declare @TableName sysname = 'ParentTable' declare @Command varchar(max) select @Command = isnull(@Command + ' union all ','') + 'select ''' + object_name(parent_object_id) + ''' where exists(select * from ' + object_name(parent_object_id) + ' where ' + col.name+ ' = ' + cast(@RowId as varchar) + ')' from sys.foreign_key_columns fkc join sys.columns col on fkc.parent_object_id = col.object_id and fkc.parent_column_id = col.column_id where object_name(referenced_object_id) = @TableName execute (@Command)
假设外键不复合.