You are on page 1of 8

FORMATTING READABLE OUTPUT

SQL * Plus to create an actual formatted report. Some of the formatting features available
in SQL* Plus include report headers and footers, formatting models for column data,
suppression of duplicate data for group reports and calculations. There are several
formatting options are available but only the most commonly used features.Most
elaborate reports are created using various application software and report generators.

Command Description

START or @ Execute a script file


COLUMN Defines the appearance of column headings and the
format of the column data
TTITLE Adds a header to the top of each report page
BTITLE Adds a footer to the bottom of each report page
BREAK Suppresses duplicated data for a specific column(s) when
presented in a sorted order
COMPUTE Performs calculations in a report based on the
AVG,SUM,COUNT,MIN or MAX statistical function

SPOOL Redirects output to a text file


COLUMN OPTIONS
HEADING Adds a column heading to a specified column
FORMAT Defines the width of columns and applies specific formats
to column containing numeric data
NULL Indicates text to be substituted for null values in
specified column
SQL * PLUS Environment Variables
UNDERLINE Specifies the symbol to be used to separate a column
heading from the contents of the column
LINESIZE Establishes the maximum number of characters that can
appear on a single line of output
PAGESIZE Establishes the maximum number of lines that can appear
on one page of output

COLUMN COMMAND

The COLUMN command can be used to format both a column heading and the data
being displayed within the column, depending on the option(s) used . The basic syntax of
the COLUMN command is

COLUMN [columnname/columnalias] [Option]

• Column Referenced is identified immediately after the COLUMN command


• If desired , a Column can be assigned a column alias.
• The option given in the COLUMN command specifies how the display will be
affected

Options Available for the COLUMN command

Option Description

FORMAT formatmodel Applies a specified format to the column


data
HEADING columnheading Indicates the heading to be used for a
specified column
NULL textmessage Identifies the text message to be displayed
in the place of NULL values

FORMAT Option:-

The format option allows to apply a format model to the data displayed within a
column. It used to format displaying values and adding additional symbols are given
below

A Book’s retail price of $54.50 was displayed as 54.5.The format option allows to
display the insignificant zero in output. Also include the dollar sign so the retail price
displayed as $54.50.

Format Code Description Example Output


9 Identifies the 999999 54
position of numeric
data (leading zeros
are suppressed)
$ Includes a floating $9999 $54
dollar sign in output
, Indicates whether to 9,999 54 or 1,214
include a comma
when needed
. Indicates the 9999.99 54.50
number of decimal
positions to be
displayed
An Identifies the width A32 Assign a width of 32
of a column in a spaces to a column
report
Create a report that one display the ISBN,title,cost and retail price for books having
retail price greater than $50. To ensure that the cost and retail price for each book
displayed with two decimal places.
COLUMN isbn FORMAT A15
COLUMN title FORMAT A35
COLUMN cost FORMAT $999.99
COLUMN retail FORMAT $999.99
SELECT isbn,title,cost,retail from books where retail >50 order by title;

HEADING OPTION:

The heading option of the COLUMN command is used to specify a column heading for a
particular column. The report column headings are simply the BOOKS table’s column
names. The heading option is similar to the use of a column alias. It provides a
substitute heading for the display of the output.

1. A column name assigned by the HEADING option of the COLUMN command cannot
be referenced in a SELECT statement.

2. A column name assigned by the HEADING option can contain line breaks. In other
words the column name can be displayed on more than one line.

COLUMN isbn FORMAT A15


COLUMN title FORMAT A35
COLUMN cost FORMAT 999.99
COLUMN retail FORMAT 999999999.99 HEADING ‘RETAIL|PRICE’
SELECT isbn,title,cost,retail from books where retail >50 order by title;

COLUMN isbn FORMAT A15


COLUMN title FORMAT A35 HEADING ‘Title’
COLUMN cost FORMAT 999.99 HEADING ‘Cost’
COLUMN retail FORMAT 999999999.99 HEADING ‘RETAIL|PRICE’
SELECT isbn,title,cost,retail from books where retail >50 order by title;

SQL > Set underline off

Modified Report Structure

COLUMN isbn FORMAT A15 HEADING ‘ISBN|--------------‘


COLUMN title FORMAT A35 HEADING ‘Title|--------------’
COLUMN cost FORMAT 999.99 HEADING ‘Cost|----------’
COLUMN retail FORMAT 999999999.99 HEADING ‘RETAIL|PRICE|------------’
SELECT isbn,title,cost,retail from books where retail >50 order by title;

NULL Option

Some reports contain NULL values. In some instances, blank spaces may be appropriate

For example , consider the orders Just Lee books ships to its customers .If management
wants a report to view the lag time for shipping current orders and substitute the words
“NOT SHIPPED’ for any NULL values that occur in the shipdate column.

SET UNDERLINE ‘=’


COLUMN order# HEADING ‘Orderr|Number’
COLUMN orderdate HEADING ‘Order|Date’
COLUMN shipdate FORMAT A12 HEADING ‘Shipped|Date’ NULL ‘Not Shipped’
SELECT order#,TO_CHAR(orderdate,’Mon DD’) orderdate, TO_CHAR(shipdate,’Mon
DD’) shipdate
From orders order by order#;

Report Headers and Footers

A headers serves as the title for a report. The header and footer of a report are set with the
TTITLE and BTITLE commands respectively. The TTITLE command indicates the text
or variables(page number) to be displayed at the top of the report. The BTITLE command
defines what is to be printed at the bottom of the report.

The syntax for both the TTITLE and BTITLE command are given below

TTITLE|BTITLE [option[text|variable]…] ON|OFF]

Option Description
CENTER Centers data to be displayed between the
left and right margins of a report
FORMAT Applies a format model to data to be
displayed(Same as COLUMN command)
LEFT Aligns data to be displayed to the left
margin of the report
RIGHT Aligns data to be displayed to the right
margin of the report
SKIP n Indicates the number of lines to skip
before the display of data resumes

[text|variable]

It means that the alignment and FORMAT options can be applied either to text entered
as literal string or to SQL* Plus variables. Some of the SQL variables.

Variable Description
SQL.LNO Current line number
SQL.PNO Current page number
SQL.RELEASE Current Oracle release number
SQL.USER Current User Name

LINESIZE:

The TTITLE command can be used to create the report heading How the center or left
and right determined . It based on LINESIZE If LINESIZE set to 100 then the center of
the report will be 50 characters from the margin.

SET PAGE SIZE 20


SET LINESIZE 100
TTITLE CENTER ‘BOOKS IN INVENTORY’ SKIP –
RIGHT ‘PAGE : ‘ FORMAT 9 SQL.PNO SKIP 2
COLUMN isbn FORMAT A15 HEADING ‘ISBN|------------‘
COLUMN title FORMAT A35 –
HEADING ‘TITLE|-------------------‘
COLUMN cost FORMAT 999.99 HEADING ‘COST|---------‘
COLUMN retail FORMAT 9999.99 HEADING ‘RETAIL |PRICE|--------‘
SELECT isbn,title,cost,retail from books where retail > 50 order by title;

BREAK Command:

The BREAK command is used to suppress duplicate data in a report. The syntax for the BREAK
command us shown.

BREAK ON columnname|columnalias [ON…] [skip n|page]

The effectiveness of the BREAK command is determined by how data are sorted in the SELECT
statement. The contents of the specified column are printed once, and the printing for all
subsequent rows is suppressed until the value in the column changes.

SET LINESIZE 32
SET PAGESIZE 27
TTITLE CENTER ‘Amount Due Per Order’ SKIP 2
BTITLE LEFT ‘Run By : ’ SQL.USER FORMAT A5
COLUMN total FORMAT 999999.99
BREAK ON customer# SKIP 1
SELECT customer#,order#,sum(retail*quantity) total from customers NATURAL JOIN orders
NATURAL JOIN orderitems NATURAL JOIN books group by customer#,order# order by
customer#;

CLEAR command

The clear command is used to clear the settings applied to the BREAK and COLUMN
commands. The settings applied to these commands are valid even after the report has been
finished. Any time that use the COLUMN or BREAK commands in a script always clear those
commands at the end of the script. So it will not affect any subsequent reports. The syntax for the
CLEAR command is
CLEAR COLUMN|BREAK

SET LINESIZE 32
SET PAGESIZE 27
TTITLE CENTER ‘Amount Due Per Order’ SKIP 2
BTITLE LEFT ‘Run By : ’ SQL.USER FORMAT A5
COLUMN total FORMAT 999999.99
BREAK ON customer# SKIP 1
SELECT customer#,order#,sum(retail*quantity) total from customers NATURAL JOIN orders
NATURAL JOIN orderitems NATURAL JOIN books group by customer#,order# order by
customer#;
CLEAR BREAK
CLEAR COLUMN

COMPUTE COMMAND:

To determine the total amount due for each customer owes by including the COMPUTING
command. The COMPUTE command can be included with the AVG,SUM,COUNT,MAX or
MIN keywords to determine average,totals,number of occurrences, the highest value or the lowest
value respectively.

COMPUTE statisticalfunction of columnname|REPORT ON groupname

SET LINESIZE 32
SET PAGESIZE 27
TTITLE CENTER ‘Amount Due Per Order’ SKIP 2
BTITLE LEFT ‘Run By : ’ SQL.USER FORMAT A5
COLUMN total FORMAT 999999.99
BREAK ON customer# SKIP 1
SELECT customer#,order#,sum(retail*quantity) total from customers NATURAL JOIN orders
NATURAL JOIN orderitems NATURAL JOIN books group by customer#,order# order by
customer#;
COMPUTE SUM OF total on customer#
CLEAR BREAK
CLEAR COLUMN

SPOOL Command

The results displayed on the computer monitor can also be redirected to a text computer file that
can than be printed through a word-processing program.
SPOOL D:\FILENAME

CLEAR COLUMN
SPOOL C:\REPORTRESULTS.TXT
SET LINESIZE 32
SET PAGESIZE 27
TTITLE CENTER ‘Amount Due Per Order’ SKIP 2
BTITLE LEFT ‘Run By : ’ SQL.USER FORMAT A5
COLUMN total FORMAT 999999.99
BREAK ON customer# SKIP 1
SELECT customer#,order#,sum(retail*quantity) total from customers NATURAL JOIN orders
NATURAL JOIN orderitems NATURAL JOIN books group by customer#,order# order by
customer#;
COMPUTE SUM OF total on customer#
SPOOL OFF
CLEAR BREAK
CLEAR COLUMN
Create view inventory as select isbn,title,retail from books with read only;

Create view prices as select isbn,title,cost retail-cost profit from books;

Create or replace view prices as select isbn,title,cost,retail,retail-cost profit from books


Select * from prices where title like '%MICKEY%';
UPDATE PRICES SET RETAIL =29.95 WHERE TITLE LIKE '%MICKEY%'

SQL> INSERT INTO PRICES VALUES(0202020202,'NEW BOOK',8.94,15.92,7.00);


INSERT INTO PRICES VALUES(0202020202,'NEW BOOK',8.94,15.92,7.00)
*
ERROR at line 1:
ORA-01733: virtual column not allowed here

create index customers on customers(lastname);

SUBSTITUTION METHOD OF UPDATE

UPDATE CUSTOMERS SET REGION =’&REGION’ WHERE STATE =’&STATE’;

You might also like