Einzelnen Beitrag anzeigen

Kostas

Registriert seit: 14. Mai 2003
Ort: Gerstrhofen
1.095 Beiträge
 
Delphi 10 Seattle Enterprise
 
#24

AW: Firebird Reihenfolge der Tabellen bei Insert mit Foreignkey

  Alt 13. Jun 2016, 23:20
Nur der Vollständigkeitshalber,

ich habe zu diesem Thema eine weitere Lösung gefunden die ebenfalls auf indices geht.
Beide StoredProcs gehören zusammen. Die Vorgehensweise ist die gleiche so ich das sehe.


Code:
create or alter procedure DB_TBLS_ROWS_FK (
    INCL_FKS smallint = 1)
returns (
    TBL_NAME varchar(200),
    FKS smallint,
    PK varchar(200),
    TBL_FIELDS bigint,
    TBL_ROWS bigint)
as
declare variable STMT_TO_EXEC varchar(200);
BEGIN
  for
    SELECT p.TBL_NAME, p.FKS
      FROM DB_TBLS_ROWS_FK_0 p order by p.FKS
    into :TBL_NAME, :FKS
  do begin
    TBL_ROWS = null;
    TBL_FIELDS = null;
    STMT_TO_EXEC = 'select count(*) as nr_of_rows from ' || :TBL_NAME;

    execute statement STMT_TO_EXEC INTO :TBL_ROWS;

    select count(*)
      from rdb$relation_fields flds
     where flds.RDB$RELATION_NAME = :TBL_NAME
      into :TBL_FIELDS;
    suspend;

    if(0 <> :INCL_FKS) then
    begin
      for
        SELECT r.RDB$FOREIGN_KEY
          FROM RDB$INDICES r
         WHERE r.RDB$RELATION_NAME= :TBL_NAME AND (r.RDB$FOREIGN_KEY IS NOT NULL)
         into :PK
      do begin
        /*TBL_NAME = null;*/
           FKS = null;
           suspend;
      end
      PK = null;
    end
  end
end



Code:
create or alter procedure DB_TBLS_ROWS_FK_0
returns (
    TBL_NAME varchar(200),
    FKS smallint)
as
BEGIN
  for SELECT DISTINCT RDB$RELATION_NAME
        FROM RDB$RELATION_FIELDS
       WHERE RDB$SYSTEM_FLAG=0
       into :TBL_NAME
  do begin
       SELECT count(RDB$INDEX_NAME)
         FROM RDB$INDICES
        WHERE RDB$RELATION_NAME= :TBL_NAME AND (RDB$FOREIGN_KEY IS NOT NULL)
       into :FKS;

       suspend;
  end
END
  Mit Zitat antworten Zitat