Format SQL Table data as Text Table

前端 未结 2 1783
粉色の甜心
粉色の甜心 2020-12-11 13:13

I have a result from Oracle database as below:

1   Chi Phí Lương   FA_Phan_Bo_Chi_Phi_Luong    8   JOHN    8   2015    (BLOB)  FA_Phan_Bo_Chi_Phi_Luong_Final         


        
相关标签:
2条回答
  • 2020-12-11 13:52

    Your text sql result:

    +---+------------------------------+-----------------------------+---+------+---+------+--------+---------------------------------------+-----------+---------------+
    | 1 | Chi Phí Lương                | FA_Phan_Bo_Chi_Phi_Luong    | 8 | JOHN | 8 | 2015 | (BLOB) | FA_Phan_Bo_Chi_Phi_Luong_Final.csv    | 09-NOV-15 | Đã Hoàn Thành |
    +---+------------------------------+-----------------------------+---+------+---+------+--------+---------------------------------------+-----------+---------------+
    | 2 | Tổng Hợp TimeSheet Nhân Viên | FA_Tong_Hop_Timesheet       | 8 | JOHN | 8 | 2015 | (BLOB) | FA_Tong_Hop_Timesheet_COM_Final.csv   | 09-NOV-15 | Đã Hoàn Thành |
    +---+------------------------------+-----------------------------+---+------+---+------+--------+---------------------------------------+-----------+---------------+
    | 3 | Tổng Hợp Doanh Thu           | FA_Tong_Hop_Doanh_Thu       | 8 | JOHN | 8 | 2015 | (BLOB) | FA_Tong_Hop_Doanh_Thu_Final.csv       | 09-NOV-15 | Đã Hoàn Thành |
    +---+------------------------------+-----------------------------+---+------+---+------+--------+---------------------------------------+-----------+---------------+
    | 4 | Chi Phí Hoạt Động Công Ty    | FA_CP_Hoat_Dong             | 8 | JOHN | 8 | 2015 | (BLOB) | FA_CP_Hoat_Dong_Final.csv             | 09-NOV-15 | Đã Hoàn Thành |
    +---+------------------------------+-----------------------------+---+------+---+------+--------+---------------------------------------+-----------+---------------+
    | 5 | Khấu Hao Máy Móc Thiết Bị    | FA_CP_Khau_Hao_Phan_Bo_MMTB | 8 | JOHN | 8 | 2015 | (BLOB) | FA_CP_Khau_Hao_Phan_Bo_MMTB_Final.csv | 09-NOV-15 | Đã Hoàn Thành |
    +---+------------------------------+-----------------------------+---+------+---+------+--------+---------------------------------------+-----------+---------------+
    

    You can generate text tables at tablesgenerator like following formats.

    You can give input as:

    1. SQL result in File -> Paste Table Data
    2. File -> Import CSV File
    3. Or you can generate table manually

    Here you can align column values to left, right, centre.

    Without Using Unicode symbols for table borders

    +----+-------------+--------+-------+-----+
    | id | name        | gender | state | age |
    +----+-------------+--------+-------+-----+
    | 1  | John Kenedy | male   | NY    | 32  |
    +----+-------------+--------+-------+-----+
    | 2  | Meresa Oslo | female | HI    | 26  |
    +----+-------------+--------+-------+-----+
    | 3  | Mike Lanes  | male   | FL    | 25  |
    +----+-------------+--------+-------+-----+
    

    Useing Unicode symbols for table borders

    ╔════╦═════════════╦════════╦═══════╦═════╗
    ║ id ║ name        ║ gender ║ state ║ age ║
    ╠════╬═════════════╬════════╬═══════╬═════╣
    ║ 1  ║ John Kenedy ║ male   ║ NY    ║ 32  ║
    ╠════╬═════════════╬════════╬═══════╬═════╣
    ║ 2  ║ Meresa Oslo ║ female ║ HI    ║ 26  ║
    ╠════╬═════════════╬════════╬═══════╬═════╣
    ║ 3  ║ Mike Lanes  ║ male   ║ FL    ║ 25  ║
    ╚════╩═════════════╩════════╩═══════╩═════╝
    

    Markdown table

    | id | name        | gender | state | age |
    |----|-------------|--------|-------|-----|
    | 1  | John Kenedy | male   | NY    | 32  |
    | 2  | Meresa Oslo | female | HI    | 26  |
    | 3  | Mike Lanes  | male   | FL    | 25  |
    
    0 讨论(0)
  • 2020-12-11 14:09

    If you're using SQLPlus or TOAD or SQLNavigator or SQL*Developer or whatever, use the features of what you are using to format output nicely.

    If you're looking for a pure-database solution (no client), I use DBMS_SQL for this. Here's the basic code (below). This version just writes the result set to DBMS_OUTPUT. You could change that to whatever you want. Also, you'd need to extend it to handle data types beyond just NUMBER, DATE, and VARCHAR2.

    DECLARE
      l_rc SYS_REFCURSOR;
    
    -- This is the procedure to format cursor output nicely
      PROCEDURE dump_cursor( p_rc IN OUT SYS_REFCURSOR) IS
        -- Dump the results of p_rc to log
    
        l_cursor                INTEGER;
        l_column_count          INTEGER;
        l_column_descriptions   sys.DBMS_SQL.desc_tab;
        l_status                INTEGER;
        l_column_value          VARCHAR2 (4000);
        l_column_width          NUMBER;
        l_rec_count             NUMBER := 0;
        l_line VARCHAR2(4000);
    
    
        FUNCTION get_length (l_column_def IN sys.DBMS_SQL.desc_rec)
          RETURN NUMBER IS
          l_width   NUMBER;
        BEGIN
          l_width   := l_column_def.col_max_len;
          l_width   :=
            CASE l_column_def.col_type
              WHEN 12 THEN /* DATE */
                          20
              WHEN 2 THEN /* NUMBER */
                         10
              ELSE l_width
            END;
          -- Don't display more than 256 characters of any one column
          l_width   := LEAST(256,GREATEST (l_width, l_column_def.col_name_len));
    
          RETURN l_width;
        END get_length;
      BEGIN
        EXECUTE IMMEDIATE 'alter session set nls_date_format=''DD-MON-YYYY HH24:MI:SS''';
    
        l_cursor := sys.DBMS_SQL.TO_CURSOR_NUMBER(p_rc);
    
        -- Describe columns
        sys.DBMS_SQL.describe_columns (c => l_cursor, col_cnt => l_column_count, desc_t => l_column_descriptions);
    
        l_line := '';
        FOR i IN 1 .. l_column_count LOOP
          l_column_width   := get_length (l_column_descriptions (i));
    
          l_line := l_line || RPAD (l_column_descriptions (i).col_name, l_column_width);
          l_line := l_line || ' ';
          DBMS_SQL.define_column (l_cursor, i, l_column_value, 4000);
        END LOOP;
    
        DBMS_OUTPUT.PUT_LINE(l_line);
    
        l_line := '';
        FOR i IN 1 .. l_column_count LOOP
          l_column_width   := get_length (l_column_descriptions (i));
    
          l_line := l_line || RPAD ('-', l_column_width, '-');
          l_line := l_line || ' ';
          DBMS_SQL.define_column (l_cursor, i, l_column_value, 4000);
        END LOOP;
    
        DBMS_OUTPUT.PUT_LINE(l_line);
    
    --   l_status   := sys.DBMS_SQL.execute (l_cursor);
    
        WHILE (sys.DBMS_SQL.fetch_rows (l_cursor) > 0) LOOP
          l_rec_count   := l_rec_count + 1;
    
        l_line := '';
          FOR i IN 1 .. l_column_count LOOP
            DBMS_SQL.COLUMN_VALUE (l_cursor, i, l_column_value);
            l_column_value := translate(l_column_value,chr(10), chr(200));
            l_column_width   := get_length (l_column_descriptions (i));
    
            IF l_column_value IS NULL THEN
              l_line := l_line || RPAD (' ', l_column_width);
            ELSE
              l_line := l_line || RPAD (l_column_value, l_column_width);
            END IF;
    
            l_line := l_line || ' ';
          END LOOP;
    
          DBMS_OUTPUT.PUT_LINE(l_line);
        END LOOP;
    
        IF l_rec_count = 0 THEN
          DBMS_OUTPUT.PUT_LINE ('No data found.');
        ELSE
          DBMS_OUTPUT.PUT_LINE (l_rec_count || ' rows returned.');
        END IF;
    
        sys.DBMS_SQL.close_cursor (l_cursor);
    
        EXECUTE IMMEDIATE 'alter session set nls_date_format=''DD-MON-YYYY''';
      EXCEPTION
        WHEN OTHERS THEN
          EXECUTE IMMEDIATE 'alter session set nls_date_format=''DD-MON-YYYY''';
    
          RAISE;
      END dump_cursor;
    
    -- This shows how to use it...
    BEGIN
    
      OPEN l_rc FOR SELECT object_id, object_name, object_type, last_ddl_time FROM dba_objects WHERE rownum <= 100;
      dump_cursor(l_rc);
    END;
    
    0 讨论(0)
提交回复
热议问题