tags:

views:

27

answers:

1

I am trying to generate a report using Oracle but am having some alignment problems. Is there a way to print values in particular columns? I am trying to achieve the following:

---------------------------------------------------------
ColA        ColB        ColC
-----     ---------   ---------
1            2           1
2            4           2
3            3           1

Currently, I am having some difficulty. I am writing the following PL/SQL command:

DECLARE

BEGIN
    DBMS_OUTPUT.PUT_LINE('-');
    DBMS_OUTPUT.PUT_LINE('----------------------------------------------------------------------------------------------------------------');
    DBMS_OUTPUT.PUT_LINE('ColA        ColB        ColC');
    DBMS_OUTPUT.PUT_LINE('-----------        ------------        -------');
    FOR record IN (SELECT * FROM TABLEA) LOOP
        DBMS_OUTPUT.PUT_LINE(record.ID || '        ' || record.TAG || '        ' || record.TIP);
    END LOOP;
END;
/

I am manually putting in eight spaces so my output is being messed up completely. Is there a better way to format the output?

+1  A: 

Never mind.. I figured it tout... Just in case anyone else is looking for an answer, using RPAD gives me what I want. For instance, I can format it nicely using the following:

DECLARE

BEGIN
    DBMS_OUTPUT.PUT_LINE('-');
    DBMS_OUTPUT.PUT_LINE(RPAD('-', 50, '-');
    DBMS_OUTPUT.PUT_LINE(RPAD('ColA', 20) || RPAD('ColB', 20) || RPAD('ColC', 20));
    DBMS_OUTPUT.PUT_LINE('-----------        ------------        -------');
    FOR record IN (SELECT * FROM TABLEA) LOOP
        DBMS_OUTPUT.PUT_LINE(RPAD(record.ID, 20) || RPAD(record.TAG, 20) || RPAD(record.TIP, 20));
    END LOOP;
END;
/
Legend
you could also use `rpad('-', 50, '-')` for your lines
be here now
@be here now: Oh.. +1 for that... thanks :) I'll modify my answer...
Legend
it is also more beautiful sometimes to declare a varchar variable, fill it with your text string, and then just do `dbms_output.put_line(s_report_string);`
be here now
@be here now: +1 again.. :) I'll keep that in mind. Thank You.
Legend