| Lesson 7 | Environment Settings for Reports |
| Objective | Define the environment settings for SQL*Plus reports. |
Up to this point, you have used SQL*Plus to run queries and adjust column attributes. In this lesson, you will learn how to define report-level environment settings so that your output looks like a professional report instead of a raw result set. These settings allow you to:
All of these behaviors are driven by SQL*Plus commands that wrap around your SELECT statement and stay in effect for the duration of your session or until you turn them off.
The commands below are specific to SQL*Plus and are used to define page layout, report headers and footers, and group-level calculations.
| Command | Description |
TTI[TLE] spec… | OFF | ON |
Defines the title that appears at the top of each page of the report.
The title can include:
TTITLE OFF to turn the title off and TTITLE ON to re-enable the last definition.
|
BTI[TLE] spec… | OFF | ON |
Defines a footer that appears at the bottom of each page.
Uses the same syntax and formatting options as TTITLE.
|
BRE[AK]
|
Controls where SQL*Plus breaks the report and what it does at each break point.
Typical syntax:
BREAK ON {column | ROW | REPORT} [SKIP n | SKIP PAGE] [DUPLICATES | NODUPLICATES]
COMPUTE to print group totals.
|
COMP[UTE]
|
Computes summary values (group functions) at break points or at the end of the report.
General syntax:
COMPUTE function [LABEL text] OF column ON {column | ROW | REPORT}
|
REPF[OOTER] [PAGE] spec… | OFF | ON |
Defines a report footer that prints once at the end of the report or at the end of each page (when PAGE is used). Syntax and formatting options match TTITLE.
|
REPH[EADER] [PAGE] spec… | OFF | ON |
Defines a report header that prints once at the beginning of the report or at the beginning of each page (when PAGE is used). Syntax and formatting options match TTITLE.
|
In practice, you will normally combine these commands with COLUMN and SET statements from the previous lessons to create a complete report layout.
The following example (based on the transcribed image) shows how TTITLE, BREAK, and COMPUTE work together to produce a customer purchasing report that groups sales by last name and prints a total for each customer.
SQL> TTITLE LEFT "Customer Purchasing" -
2 SKIP 1 "Pg." SQL.PNO SKIP 2
SQL> COMPUTE SUM OF TOTAL_SALE ON LASTNAME
SQL> BREAK ON LASTNAME SKIP 1
SQL> SELECT
2 C.LASTNAME,
3 CS.SALES_DATE,
4 CS.TOTAL_SALE_AMOUNT TOTAL_SALE
5 FROM CUSTOMER C,
6 CUSTOMER_SALE CS
7 WHERE C.CUST_ID = CS.CUST_ID
8 ORDER BY C.LASTNAME, CS.SALES_DATE;
Customer Purchasing
Pg. 1
LASTNAME SALES_DATE TOTAL_SALE
------------------------ ---------- ----------
Black 12-DEC-99 61.90
01-MAR-00 108.03
************************
sum 169.93
Lee 14-JUL-99 52.66
-) at the end of the first line continues the command on the next line.LEFT "Customer Purchasing" places the title at the left edge of the first line.SKIP 1 "Pg." SQL.PNO SKIP 2 prints Pg. followed by the current page number (using the predefined SQL.PNO variable), then skips two lines before the column headings.TOTAL_SALE column for each group of rows sharing the same LASTNAME.LASTNAME, suppresses duplicate printing of the last name within the group, and skips one line after the group total line.SELECT statement is ordered by LASTNAME and SALES_DATE so that related rows appear together and totals are correctly computed for each customer.TTITLE for page title, BREAK for grouping, and COMPUTE for totals—is the foundation for many classic SQL*Plus report layouts.
When designing a report, it helps to think of each SQL*Plus environment command in terms of the part of the report it controls:
PAGE is used).PAGE is used).PAGESIZE and LINESIZE, which determine page length and line width.All title and header/footer commands share a common syntax pattern: a command name followed by one or more pairs of positioning/formatting clauses and character 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 ...
Examples of position_clause values include:
LEFT, CENTER, RIGHT – horizontal alignmentSKIP n – skip n lines before printing the next partCOL n – start the next part at column nBOLD – display the text in bold (supported in many SQL*Plus environments)By combining these clauses, you can construct detailed page headers and footers that include report names, dates, page numbers, and even dynamic values from SQL*Plus substitution variables.
In the next lesson, you will build on these settings by learning how to spool report output to files so that formatted SQL*Plus reports can be archived, printed, or shared with other users.