Vous êtes sur la page 1sur 24

SQL REPORTS

Formatting Columns
Through the SQL*Plus COLUMN command, you can change the column headings and reformat the column data in your query results.

Changing Column Headings


When displaying column headings, you can either use the default heading or you can change it using the COLUMN command. The following sections describe how default headings are derived and how to alter them using the COLUMN command. You can define a more useful column heading with the HEADING clause of the COLUMN command, in the following format: Syntax: COLUMN column_name HEADING column_heading Ex: COLUMN SALARY HEADING 'MONTHLY SALARY' COLUMN COMM HEADING COMMISSION SELECT SAL,COMM Output:
MONTHLY SALARY -----------------------2450 5000 2500 2500 2500 2500 2500 1600 2850 950 1500 11 rows selected. COMMISSION ----------------0 0

FROM EMP;

300

Splitting a Column Heading


To give the columns SALARY and LAST_NAME the headings MONTHLY SALARY and LAST NAME respectively, and to split the new headings onto two lines, enter Syntax: COLUMN column_name HEADING column_heading1| column_heading1 Ex: SQL> COLUMN SAL HEADING MONTHLY|SALARY SQL> COLUMN ENAME HEADING EMP|NAME SQL> SELECT ENAME,SAL FROM EMP; Output: NAME -------------------clark king smith adams ford scott johns allen blake james turner MONTHLY SALARY ---------------2450 5000 2500 2500 2500 2500 2500 1600 2850 950 1500

11 rows selected.

Setting the Underline Character


To change the character used to underline headings to an equal sign and rerun the query, enter the following commands: Syntax: SET UNDERLINE Symbol; EX: SET UNDERLINE =;

Output: SQL> SELECT ENAME,SAL FROM EMP;

MONTHLI NAME SALARY ==================== ========== clark 2450 king 5000 smith 2500 adams 2500 ford 2500 scott 2500 johns 2500 allen 1600 blake 2850 james 950 turner 1500 11 Rows selected.

Formatting a NUMBER Column


When displaying NUMBER columns, you can either accept the SQL*Plus default display width or you can change it using the COLUMN command. Later sections describe the default display and how you can alter it with the COLUMN command. The format model will stay in effect until you enter a new one, reset the column's format with COLUMN column_name CLEAR To display SALARY with a dollar sign, a comma, and the numeral zero instead of a blank for any zero values, enter the following command: Syntax: COLUMN COL_NAME FORMAT SPECIFIED FORMAT Ex: COLUMN SAL FORMAT $99,990; SELECT ENAME,SAL FROM EMP;

Output: MONTHLY NAME SALARY ==================== ======== clark $2,450 king $5,000 smith $2,500 adams $2,500 ford $2,500 scott $2,500 johns $2,500 allen $1,600 blake $2,850 james $950 turner $1,500 11 rows selected.

Changing the Default Display


You can change the displayed width of a datatype or DATE, by using the COLUMN command with a format model consisting of the letter A (for alphanumeric) followed by a number representing the width of the column in characters. Within the COLUMN command, identify the column you want to format and the model you want to use: Syntax: COLUMN column_name FORMAT model Ex: COLUMN ENAME FORMAT A4; SELECT ENAME FROM EMP; Output: NAME ==== clar k king smit h adam s ford scot t john s alle n blak e jame s 11 rows selected.

If the WRAP variable of the SET command is set to ON (its default value), the employee names wrap to the next line after the fourth character. If WRAP is set to OFF, the names are truncated (cut off) after the fourth character.

Copying Column Display Attributes


When you want to give more than one column the same display attributes, you can reduce the length of the commands you must enter by using the LIKE clause of the COLUMN command. The LIKE clause tells SQL*Plus to copy the display attributes of a previously defined column to the new column, except for changes made by other clauses in the same command.

Syntax: COLUMN COL_NAME LIKE DISPLAY_COL_NAME HEADING DISPLAY_TEXT Ex: COLUMN COMM LIKE SAL HEADING BONUS; SELECT ENAME,SAL,COMM FROM EMP; Output: MONTHLI NAME SALARY BONUS

===== ======== ======== clark king smith adams ford scott johns allen blake james turner $2,450 $5,000 $2,500 $2,500 $2,500 $2,500 $2,500 $1,600 $2,850 $950 $1,500 $0 $300 $0 $0

11 rows selected.

Listing and Resetting Column Display Attributes


To list the current display attributes for a given column, use the COLUMN command followed by the column name only, as shown: Syntax: COLUMN column_name Ex: SQL> COLUMN ENAME; Output: COLUMN ENAME ON HEADING 'EMP|NAME' headsep '|' FORMAT A3

To list the current display attributes for all columns, enter the COLUMN command with no column names or clauses after it: Syntax: COLUMN To reset the display attributes for a column to their default values, use the CLEAR clause of the COLUMN command as shown: Syntax: COLUMN [column_name] CLEAR Ex: CLEAR COLUMNS; Output: Columns cleared

Suppressing and Restoring Column Display Attributes


You can suppress and restore the display attributes you have given a specific column. To suppress a column's display attributes, enter a COLUMN command in the following form: Syntax: COLUMN column_name OFF Ex: COLUMN ENAME OFF;

Output: ENAME ==================== king smith adams ford scott allen james turner

8 rows selected.

OFF tells SQL*Plus to use the default display attributes for the column, but does not remove the attributes you have defined through the COLUMN command. To restore the attributes you defined through COLUMN, use the ON clause:

Syntax: COLUMN column_name ON; EX: COLUMN ENAME ON;

Suppressing Duplicate Values in Break Columns


The BREAK command suppresses duplicate values by default in the column or expression you name. Thus, to suppress the duplicate values in a column specified in an ORDER BY clause, use the BREAK command in its simplest form: Syntax: BREAK ON COL_NAME; Ex: SQL> BREAK ON DEPTNO; SQL> SELECT ENAME,SAL,DEPTNO FROM EMP; Output:

EMP MONTHLY NAME SALARY ====== ========== king smith $5000.00 $2500.00

DEPTNO ========== 10 20

adams $2500.00 ford scott allen james $2500.00 $2500.00 $1600.00 $950.00 30

turner $1500.00

8 rows selected.

10

Inserting Space when a Break Column's Value Changes


You can insert blank lines or begin a new page each time the value changes in the break column. To insert n blank lines, use the BREAK command in the following form: Syntax: BREAK ON break_column SKIP n Ex: BREAK ON DEPTNO SKIP 1; SELECT ENAME,DEPTNO FROM EMP; Output: ENAME DEPTNO

----------------- ------------king 10

smith adams ford scott

20

allen james turner

30

8 rows selected.

11

To skip a page, use the command in this form:


Syntax: BREAK ON break_column SKIP PAGE Ex: SQL>BREAK ON deptno SKIP PAGE SQL> SELECT * FROM EMP WHERE EMPNO<=7500;

Output:

EMPNO ENAME

JOB

MGR

HIREDATE

SAL

COMM ----------

DEPTNO -----------20

--------- -------------------- ---------- ---------- --------- ---------- ---------7369 smith clerk 7902 17-DEC-80 2500

EMPNO ENAME

JOB

MGR

HIREDATE

SAL

COMM

DEPTNO ------------30

--------- ---------------- ---------- ---------- ------------------ ---------- ---------7499 allen sales man 7698 20-FEB-81 1600 300

12

Inserting Space after Every Row


You may wish to insert blank lines or a blank page after every row. To skip n lines after every row, use BREAK in the following form: Syntax: BREAK ON ROW SKIP n Ex: BREAK ON ROW SKIP 2 SELECT * FROM EMP WHERE EMPNO<=7500; Output:
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----------20

---------- -------------------- ---------- ---------- --------- ---------- ---------- ---------7369 smith clerk 7902 17-DEC-80 2500

7499

allen

sales man 7698 20-FEB-81

1600

300

30

13

To skip a page after every row


Syntax: BREAK ON ROW SKIP PAGE Ex: SQL> BREAK ON ROW SKIP PAGE SQL> SELECT * FROM EMP WHERE EMPNO<=7500;

Output:
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----------20

---------- -------------------- ---------- ---------- --------- ---------- ---------- ---------7369 smith clerk 7902 17-DEC-80 2500

EMPNO ENAME

JOB

MGR

HIREDATE

SAL

COMM

DEPTNO ----------30

---------- -------------------- ---------- ---------- ----------------7499 allen sales man 7698 20-FEB-81

---------- ---------1600 300

14

To Skip Page Based On Column


Syntax: BREAK ON COL_NAME SKIP PAGE ON TABLE_NAME Ex: SQL> BREAK ON DEPTNO SKIP PAGE ON EMP SQL> SELECT * FROM EMP; Output:
EMPNO ENAME JOB MGR HIREDATE SAL ----2450 5000 COMM DEPTNO

---------- -------------------- ---------- ---------- ---------------7782 7839 clark king manager 7839 09-JUN-81 president 17-NOV-81

---------- ---------0 0 10

EMPNO ---------7369 7876 7902 7788 7566

ENAME ---------smith adams ford scott johns

JOB -----clerk clerk analyst analyst manager

MGR

HIREDATE

SAL

COMM

DEPTNO ---------20

---------- --------------7902 17-DEC-80 7788 23-MAY-87 7566 03-DEC-81 7566 19-APR-87 7839 02-APR-81

---------- ---------2500 2500 2500 2500 2500

EMPNO ----------

ENAME ----------

JOB -----------sales man manager clerk sales man

MGR

HIREDATE

SAL

COMM

DEPTNO ---------30

---------- --------------7698 20-FEB-81 7839 01-MAY-81 7698 03-DEC-81 7698 08-SEP-81

---------- ---------1600 2850 950 1500 0 300

7499 allen 7698 blake 7900 james 7844 turner

15

Display Title on Every Page

Syntax: TTITLE FORMAT NO DISPLAY_STRING Ex: SQL> TTITLE FORMAT 9 'WELCOME' SQL> SELECT * FROM EMP WHERE EMPNO<=7500;

Output: WELCOME EMPNO ENAME ---------- -----------7369 smith JOB MGR HIREDATE SAL COMM DEPTNO

---------- ---------- ------------------ ---------- ---------- -------------clerk 7902 17-DEC-80 2500 20

WELCOME EMPNO ENAME ---------- -----------7499 allen JOB MGR HIREDATE SAL COMM DEPTNO

---------- ---------- ------------------sales man 7698 20-FEB-81

--------- ---------- -------------1600 300 30

16

Ex: SQL> TTITLE FORMAT 9 'PAGE:'SQL.PNO SQL> SELECT * FROM EMP WHERE EMPNO<=7500; PAGE: 1 EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO -----------------20

---------- -------------------- ---------- ---------- ----------------7369 smith clerk 7902 17-DEC-80

---------2500

PAGE: 2 EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

---------- -------------------- ---------- ---------- ---------------- ---------- ---------- -------------7499 allen sales man 7698 20-FEB-81 1600 300 30

17

Computing and Printing Subtotals


To compute the total of SALARY by department Syntax: BREAK ON COL_NAME; COMPUTE FUN_NAME OF COL_NAME ON BREAKING_COL_NAME Ex: SQL>BREAK ON DEPTNO; SQL>COMPUTE SUM OF SAL ON DEPTNO; SQL>SELECT * FROM EMP; Output: EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------10

---------- ------------ ------- ---------- -------------------- ---------- ---------7839 king president 17-NOV-81 5000 ---------5000 7369 smith clerk 7902 17-DEC-80 2500 2500 2500 2500 ---------10000 7499 allen 7900 james 7844 turner sales man 7698 20-FEB-81 clerk salesman 7698 03-DEC-81 7698 08-SEP-81 1600 950 1500 ---------4050 8 rows selected. sum sum 300 0

********** sum 20

7876 adams clerk 7902 ford 7788 scott analyst analyst

7788 23-MAY-87 7566 7566 03-DEC-81 19-APR-87

**********

30

0 **********

18

Computing and Printing a Grand Total


To calculate and print the grand total of salaries for all sales people and change the compute label, first enter the following BREAK and COMPUTE commands: Syntax: BREAK ON REPORT COMPUTE FUN_NAME LABEL LABEL_NAME OF COL_NAME ON REPORT Ex: SQL>BREAK ON REPORT; SQL>COMPUTE SUM LABEL TOTAL OF SAL ON REPORT; SQL>SELECT * FROM EMP; Output:

EMPNO ENAME JOB

MGR

HIREDATE

SAL

COMM

DEPTNO ---------10 20

---------- ------------ ------- ---------- -------------------- ---------- ---------7839 king president 7902 17-NOV-81 17-DEC-80 5000 2500 2500 2500 2500 1600 950 1500 ------------19000 0 300 0

7369 smith clerk 7876 Adams clerk 7902 ford 7788 Scott 7499 Allen 7900 James 7844 turner TOTAL

7788 23-MAY-87

analyst 7566 03-DEC-81 analyst sman clerk sman 7566 19-APR-87

7698 20-FEB-81 7698 7698 03-DEC-81 08-SEP-81

30

19

Computing Multiple Summary Lines on the Same Break Column


To compute the average and sum of salaries for the sales department, first enter the following BREAK and COMPUTE commands: Syntax: BREAK ON COL_NAME COMPUTE fun_name1,fun_name2fun_namen ON breaked_col_name Ex: SQL> BREAK ON DEPTNO; SQL> COMPUTE SUM AVG OF SAL ON DEPTNO; SQL> SELECT ENAME,SAL FROM EMP; Output:

OF col_name

EMP MONTHLY NAME SALARY ====== ========== king smith adams ford scott allen james turner $5000.00 $2500.00 $2500.00 $2500.00 $2500.00 $1600.00 $950.00 $1500.00 ---------------SUM AVG $19050.00 2381.25

20

Listing and Removing COMPUTE Definitions


You can list your current COMPUTE definitions by entering the COMPUTE command with no clauses: Syntax: COMPUTE Ex: SQL>COMPUTE;

Output:

To remove all COMPUTE definitions and the accompanying BREAK definition, enter the following commands: Syntax: CLEAR COMPUTES Ex: SQL> CLEAR COMPUTES Output: COMPUTES CLEARED

21

Listing and Removing BREAKS Definitions


You may wish to place the commands CLEAR BREAKS and CLEAR COMPUTES at the beginning of every script to ensure that previously entered BREAK and COMPUTE commands will not affect queries you run in a given file. Syntax: BREAKS Ex: SQL> BREAKS

To remove all BREAK definitions, enter the following commands:

Syntax: CLEAR BREAKS Ex: SQL> CLEAR BREAKS

OUTPUT: BREAKS CLEAREDS

22

Setting the Top and Bottom Titles and Headers and Footers
As you have already seen, you can set a title to display at the top of each page of a report. You can also set a title to display at the bottom of each page. The TTITLE command defines the top title; the BTITLE command defines the bottom title. You can also set a header and footer for each report. The REPHEADER command defines the report header; the REPFOOTER command defines the report footer. A TTITLE, BTITLE, REPHEADER or REPFOOTER command consists of the command name followed by one or more clauses specifying a position or format and a CHAR value you wish to place in that position or give that format. You can include multiple sets of clauses and CHAR values: TTITLE position_clause(s) char_value position_clause(s) char_value ... BTITLE position_clause(s) char_value position_clause(s) char_value ... REPHEADER position_clause(s) char_value position_clause(s) char_value ... REPFOOTER position_clause(s) char_value position_clause(s) char_value ...

Syntax:
TTITLE ALINMENT STRING TO BE DISPLAY ON THE TOP BTITLE ALINMENT STRING TO BE DISPLAY ON THE BOTTOM Ex: SQL> TTITLE CENTER WELCOME SQL> BTITLE CENTER THANK YOU SQL> SELECT * FROM EMP WHERE EMPNO<=7500; Output: WELCOME

EMPNO ENAME JOB MGR HIREDATE

SAL

COMM

DEPTNO

---------- ------------ ------- ---------- -------------------- ---------- ---------- -----------7369 smith 7499 allen clerk sman 7902 17-DEC-80 7698 20-FEB-81
THANK YOU

2500 1600 300

20 30

23

Listing, Suppressing, and Restoring Page Title Definitions


To list a page title definition, enter the appropriate title command with no clauses: Syntax: TTITLE BTITLE To suppress a title definition, enter: Syntax: TTITLE OFF BTITLE OFF These commands cause SQL*Plus to cease displaying titles on reports, but do not clear the current definitions of the titles. You may restore the current definitions by entering: Syntax: TTITLE ON BTITLE ON

24

Vous aimerez peut-être aussi