How can I get the table description (fields and types) from Firebird with dbExpress

后端 未结 4 1856
你的背包
你的背包 2021-02-04 07:42

I have written a tool for displaying database structures using the GetTableNames and GetFieldNames methods of TSQLConnection. How can I get the types of each field name similar

4条回答
  •  轮回少年
    2021-02-04 07:59

    Use direct access to RDB$ tables. For example:

    SELECT * FROM rdb$relations
    

    will give you a list of all tables in a database.

    SELECT
      *
    FROM
      rdb$relation_fields rf JOIN rdb$fields f
        ON f.rdb$field_name = rf.rdb$field_source
    WHERE
      rf.rdb$relation_name = :RN
    

    will result in a list of all fields of given table with information of field type. Param RN is a name of the table.

    Using information from RDB$tables one can easily construct DDL statement. The query below gives you a hint how to do it:

    SELECT
      TRIM(rf.rdb$field_name) || ' ' ||
      IIF(rdb$field_source LIKE 'RDB$%',
      DECODE(f.rdb$field_type, 
        8,  'INTEGER', 
        12, 'DATE', 
        37, 'VARCHAR', 
        14, 'CHAR', 
        7,  'SMALLINT'),
      TRIM(rdb$field_source)) ||
      IIF((rdb$field_source LIKE 'RDB$%') AND (f.rdb$field_type IN (37, 14)),
        '(' || f.rdb$field_length || ')',
        '') ||
      IIF((f.rdb$null_flag = 1) OR (rf.rdb$null_flag = 1), 
        ' NOT NULL', '')
    FROM
      rdb$relation_fields rf JOIN rdb$fields f
        ON f.rdb$field_name = rf.rdb$field_source
    WHERE
      rf.rdb$relation_name = ''
    

提交回复
热议问题