Hallo Jobo,
dieser Select alleine liefert leider nicht die richtige Reihenfolge, zudem auch die Tabellen mehrfach wenn sie mehrere Foreign Key haben.
Ich habe auch soeben versucht in WHERE rc.RDB$RELATION_NAME den Tabellenname zu übergeben, leider auch ohne Erfolg.
Code:
SELECT rc.RDB$RELATION_NAME as Constraint_Tablename,
rc.RDB$CONSTRAINT_NAME,
s.RDB$FIELD_NAME AS field_name,
rc.RDB$CONSTRAINT_TYPE AS constraint_type,
refc.RDB$UPDATE_RULE AS on_update,
refc.RDB$DELETE_RULE AS on_delete,
refc.RDB$MATCH_OPTION AS match_type,
i2.RDB$RELATION_NAME AS references_table,
s2.RDB$FIELD_NAME AS references_field,
(s.RDB$FIELD_POSITION + 1) AS field_position
FROM RDB$INDEX_SEGMENTS s
LEFT JOIN RDB$RELATION_CONSTRAINTS rc
ON rc.RDB$INDEX_NAME = s.RDB$INDEX_NAME
LEFT JOIN RDB$REF_CONSTRAINTS refc
ON rc.RDB$CONSTRAINT_NAME = refc.RDB$CONSTRAINT_NAME
LEFT JOIN RDB$RELATION_CONSTRAINTS rc2
ON rc2.RDB$CONSTRAINT_NAME = refc.RDB$CONST_NAME_UQ
LEFT JOIN RDB$INDICES i2
ON i2.RDB$INDEX_NAME = rc2.RDB$INDEX_NAME
LEFT JOIN RDB$INDEX_SEGMENTS s2
ON i2.RDB$INDEX_NAME = s2.RDB$INDEX_NAME AND s.RDB$FIELD_POSITION = s2.RDB$FIELD_POSITION
WHERE -- rc.RDB$RELATION_NAME='TESTTABLE' -- table name
-- AND rc.RDB$CONSTRAINT_NAME='FK_B' -- constraint name
-- AND rc.RDB$CONSTRAINT_TYPE IS NOT NULL
rc.RDB$CONSTRAINT_TYPE = 'FOREIGN KEY'
and rc.RDB$RELATION_NAME in (SELECT RDB$RELATION_NAME
FROM RDB$RELATIONS r
WHERE RDB$SYSTEM_FLAG=0
AND RDB$RELATION_TYPE = 0 /* nur Tabellen */
AND RDB$RELATION_NAME <> 'TABLES_ORDERED')
ORDER BY s.RDB$FIELD_POSITION
Gruß Kostas