You are on page 1of 8

ASIC SQL REPORTS AND COMMANDS

SETTING FEEDBACK
SQLPLUS tells you how many rows it found in a table. This is called feedback.
You can turn it off or on. The default value is 6 rows.

Set feedback off turns it off


Set feedback on turns it on
Set feedback 25 sets the minimum number of rows to 25 for
feedback to work

SET and SHOW


This is an SQLPLUS command used to tell SQLPLUS how to act.

It has a counterpart named SHOW to allow you to see the instructions you've
typed, e.g.

Show feedback

Set numwidth 10 will set the numberwidth for number columns to 10.
Columns with more than 10 numbers will not display correctly.

REPORT COMMANDS
Reports are produced by storing commands to a file that can be executed by
using the SQLPLUS START command. You may use whatever editor you
prefer to create the report file.

REMARK
This command can be shortened to REM. SQLPLUS ignores anything on a
line that begins with REM to allow you to document your program file.

SET HEADSEP
This is a Heading Separator command to tell SQLPLUS how you will indicate
where you wish to break a page or column heading that is more than one line.
The default is the vertical bar (|), but if your keyboard does not have this, use
this command to select another character like the exclamation point.
Set headsep !

TTITLE and BTITLE


The TTITLE stands for top title at the top of each page of a report. Note this
example produces a two line split title that is centered on each page of a
report. It also default places the day, month, and date the report was run in
the upper-left corner of the top title, and the page number in the upper-right
corner of the top title. See also the REPHEADER and REPFOOTER
commands to control placement of title information for Oracle 7.2 and later.

ttitle 'Sales by Month During 2000 | Second Half of the Year'

The BTITLE command is for the bottom of each page and centers the output
on each page.

btitle 'Doug's Report'

COLUMN
This command allows you to change the heading and format of a column that
is in a SELECT statement. This example produces a two-line heading for the
column named ITEM, and the following command sets ITEM to alpha, 18
columns of output, with all characters after 18 being word wrapped to the next
line:

Column Item heading 'What Was | Sold'


Column Item format A18

If you want all after 18 characters to be truncated, the command is:

Column Item truncated

Combining these commands:


Column Item heading 'What Was | Sold' format a18 truncated

Example of formatting a number--this will always print a zero in the dollars


position for amounts that are less than 1.00:

Column Rate format 90.99

CREATING A NEW COLUMN


SELECT Quantity, Rate, Quantity * Rate "Ext" from Ledger
Will create a new column that consists of Quantity * Rate and name the
column Ext. This is a virtual column.

BREAK ON
To produce grouping of items as in a control break report, use the BREAK ON
command, in conjunction with the ORDER BY option in the SELECT
command.

Break on Item skip 2

SQLPLUS will examine each row and track the value in Item. When the value
for Item changes, SQLPLUS will skip two lines. Also the value for Item is
printed on the first line of its section to eliminate duplicate printing of each of
these items for every row in each section.

If you want a grand total for the report, use the command:

Break on Item skip 2 on report

If you just use BREAK ON report, then the second BREAK ON command will
override the first one.

COMPUTE SUM
This calculates totals for each section of the report. It will always total for the
field specified in the BREAK ON command. This will compute the total sum of
the values of Ext for each Item. This command must be used in conjunction
with the BREAK ON command. Treat them as a single unit of two related
commands.

Break on Item skip 2 on report


Compute sum of Ext on Item

o Ever BREAKON must have a related ORDER BY


o Every COMPUTE SUM must have a related BREAK ON

SET LINESIZE, SET PAGESIZE, SET NEWPAGE


These commands control the dimensions of your report’s output.

SET LINESIZE governs the maximum number of characters to print on a


single line.
o If you put more columns in your SQL query than will fit onto a
single LINESIZE, the additional columns wrap to the next line.
o LINESIZE also determines the centering of the TTITLE.
o Usually you use 70 or 79 columns of output depending on the size
of the font that your printer uses by default. Don’t use 80 because
your output file needs space to store the line return character that
is invisible to the human eye.

SET PAGESIZE sets the total number of lines of output on a page including
all headings and blank line prints. You usually use 66 lines for output, but
sometimes 60 works better for some printers.

SET NEWPAGE actually sets blank lines that print before the top line

o Use SET NEWPAGE 0 to cause the computer to produce a top-


of-form character (hex 13), as most printers will move to the top of
a new page before beginning to print.
o Use this approach to control output for dot-matrix printers.

SPOOL
Spooling a file is writing the file to a print queue To name the output file for
your report, use the SPOOL command. We usually use a default filename
extension of lst (stands for listing).

Spool myoutputfile.lst

o Place this command just before your SELECT command in a


report producing program.
o Place SPOOL OFF directly after the SELECT command. This
turns spooling off so that output quits going to the output file.
o You can also use spooling to create a program file, but all of the
output including messages produced by SQLPLUS will go to the
file.
o This is also a good way to capture error messages.

/* */ REMARKS
Use this option to embed comments in an SQL statement. Everything within
the symbols is treated as a remark.
COMMAND LINE EDITOR
SQLPLUS has a scratchpad buffer where is stores commands. It only stores
the most recent command. Consider the example command:

SQL select Featuer, Section, Page


2 from NEWSPAPER
3 where Section = 'F';

select Featuer, Section, Page


*
ERROR at line 1:
ORA-00904: invalid column name

You can LIST the contents of the editor to correct the typing error.

SQL list
1 select Featuer, Section, Page
2 from NEWSPAPER
3* where Section = 'F'

To List the line to be edited, give the LIST 1 command.

SQL list 1
1* select Featuer, Section, Page

To change the value, use the Change command to type the original spelling
and the new spelling divided by delimiters (here we use the / symbol to delimit
the text, but you could also use any other special character like the $ symbol).
I also abbreviated Change with the letter C.

SQL c/Featuer/Feature/
1* select Feature, Section, Page

Use the / command to run the query again.

SQL /
FEATURE S PAGE
--------------- - ----------
Births F 7
Classified F 8
Obituaries F 6
Doctor Is In F 6
There are other options to use with the buffer editor. I tend to put my code in
an ASCII file and edit the file directly as opposed to using the buffer editor.

SET PAUSE, SETTING the EDITOR and HOST


You can cause the report to display exactly one full screen at a time. I put
these commands in my login.sql file so that they automatically execute every
time I load SQLPLUS. I use output of 23 lines for the screen.

I also define the editor to use for my files to be the vi editor. Put all of these
commands in your login.sql file.

set pause 'More . . .'


set pagesize 23
set pause on
define_editor = "vi"

You can temporarily host out to the operating system with the HOST
command. This is useful to copy files, etc. You execute the EXIT command to
return to SQLPLUS.

START
This command is used to execute an SQL report file. Use the filename
extension of SQL for your program files. Example:

Start myreportfile.sql

CHECKING THE SQLPLUS ENVIRONMENT


CHECKING VALUES OF COMMANDS
You can check the values of various commands, usually by just typing the
command. Examples:

Column Item will display the instructions about the column named Item
Ttitle will display instructions about the TTITLE command value
Btitle will display instructions about the BTITLE command value
Compute will display information about the COMPUTE command value
USING SHOW
Use the SHOW command to display information about values that were set
with the SET command.

Show headsep
Show linesize
Show pagesize
Show newpage

DISABLING COMMANDS and CLEARING VALUES


Ttitle off will turn this command off.
Btitle off will turn this command off.
Clear columns will clear the values stored for all columns.
Clear breaks will clear all values for breaks.
Clear computes will clear all values for computes.

EXAMPLE 
 

SPOOL TEMP
CLEAR COLUMNS
CLEAR BREAKS
CLEAR COMPUTES

COLUMN DEPARTMENT_ID HEADING DEPARTMENT


COLUMN LAST_NAME HEADING 'LAST NAME'
COLUMN SALARY HEADING 'MONTHLY SALARY' FORMAT $99,999

BREAK ON DEPARTMENT_ID SKIP 1 ON REPORT


COMPUTE SUM OF SALARY ON DEPARTMENT_ID
COMPUTE SUM OF SALARY ON REPORT

SET PAGESIZE 24
SET NEWPAGE 0
SET LINESIZE 70

TTITLE CENTER 'A C M E W I D G E T' SKIP 2 -


LEFT 'EMPLOYEE REPORT' RIGHT 'PAGE:' -
FORMAT 999 SQL.PNO SKIP 2
BTITLE CENTER 'COMPANY CONFIDENTIAL'

SELECT DEPARTMENT_ID, LAST_NAME, SALARY


FROM EMP_DETAILS_VIEW
WHERE SALARY>12000
ORDER BY DEPARTMENT_ID;

SPOOL OFF

If you do not want to see the output on your screen, you can also add SET TERMOUT OFF to
the beginning of the file and SET TERMOUT ON to the end of the file. Save and close the file in
your text editor (you will automatically return to SQL*Plus). Now, run the script EMPRPT:

@EMPRPT

SQL*Plus displays the output on your screen (unless you set TERMOUT to OFF), and spools it
to the file TEMP:

A C M E W I D G E T

EMPLOYEE REPORT PAGE: 1

DEPARTMENT LAST NAME MONTHLY SALARY


---------- ------------------------- --------------
20 Hartstein $13,000
********** --------------
sum $13,000

80 Russell $14,000
Partners $13,500
********** --------------
sum $27,500

90 King $24,000
Kochhar $17,000
De Haan $17,000
********** --------------
sum $58,000

--------------
sum $98,500
COMPANY CONFIDENTIAL

6 rows selected.
 

You might also like