Reports With SQL*Plus

SQL*Plus can be used to generate reports. Through the use of SELECT statements you can perform queries into the ORACLE database and then alter the display to create polished reports. Some elements of a report over which you have control are: titles, column headings, subtotals and grand totals, reformatting of numbers and text and column computations. Getting SQL*Plus to produce reports according to your needs requires only a few simple commands.

The script is made up of both SQL and SQL*Plus commands. How can you tell the difference? Beginning with the SELECT statement and ending with the semi-colon is SQL which talks to the ORACLE database. The remaining commands are SQL*Plus which are used to format the results of the query into a report.

NOTE: If you are using telnet through pigseye to access ORACLE, you may have difficulty viewing on your screen the reports you produce. Table(s) mentioned in the SELECT statement are printed following the generation of the report. This may prevent your seeing the report because there is no scroll bar to scroll back up to see the report. When you begin your SQL*Plus session, issue the following command:

SQL>SET PAUSE 'More. . .'

SQL>SET PAUSE ON

Setting PAUSE ON will cause SQL*Plus to display one full screen of information and then it waits for you to press enter before it displays the next screen. More… will appear in the lower left corner until you press enter.

Suppose we want an annual salary report. We want all employees from the EMPLOYEE listed on the report, their job title, department number and annual salary. We want employees grouped by department. We want subtotals of annual salary for each department, then a total annual salary at the end of the report. Sound a bit intimidating? Don't worry, we'll build this report in small steps and explain each line as we go. The best place to start is with your SELECT statement. Once you are sure it returns the information you need, then you can begin adding SQL*Plus commands to format the report.

How should we write the SELECT statement that will return the data needed, grouped as we want it?

SQL>SELECT DEPTNO, NAME, JOB, SALARY*12

FROM EMPLOYEE

ORDER BY DEPTNO;

DEPTNO

NAME

JOB

SALARY*12

10

Wilson

Clrk

20400

10

Chen

Mngr

34800

30

Reed

Anlt

42000

30

Watson

Mngr

54000

40

Smith

Slsm

30000

40

Allen

Mngr

45600

40

Schwartz

Slsm

51000

50

Turner

Clrk

21600

50

Ramirez

Mngr

43800

60

McDonnel

Clrk

19500

60

Simpson

Drvr

9900

60

Di Salvo

Spvr

32400

Now we begin building the report

With the SQL*Plus command SPOOL, we can store query results in a file to be edited or printed later. We will call this file repfile. Since only SQL commands are usually stored in the buffer, we need to begin by setting up a buffer that will store our SQL*Plus commands along with the SQL commands. The command to set up this buffer looks like this:  

SQL> SET BUFFER <name>

I will use 'a' for my buffer name.

SQL>SET BUFFER a

Then issue the INPUT command so that we may begin putting our file into the buffer.

SQL>i

1 SPOOL repfile

2 SELECT DEPTNO, NAME, JOB, SALARY*12

3 FROM EMPLOYEE

4 ORDER BY DEPTNO;

5 SPOOL OUT

Hit enter twice to return to the SQL> prompt.

Save the query as empreport. Don't confuse this file with repfile. Empreport will contain the SQL SELECT statement and SQL*Plus commands that produce the report. Repfile will contain the report produced by empreport.

SQL>save empreport

Now run empreport. Since this file begins with a SQL*Plus command, we must use the start or @ command.

SQL>@empreport

The results table looks the same on the screen as previously, but the file repfile now contains the results table. SPOOL <file_name> tells SQL*Plus to store the query results, formatted as you specify, in the file <file_name>. If you do not follow the file name with a period and an extension, the default file extension LST will be added. This file is stored in your directory on csis. You may use WS-FTP to copy it to your computer later. SPOOL OFF turns spooling off. You may receive an error message telling you there is no default destination and to identify your printer. Ignore this message. Your result file is being saved. SPOOL can also be used to send results to a printer.

Let's add a title at the top of the report.

SQL>get empreport

1 spool repfile

2 select deptno, name, job, salary*12

3 from employee

4 order by deptno;

5 spool off

We need to add a line after line 1. LIST line 1, then use the INPUT command to enter the title.

SQL>l1

1*Spool repfile

SQL>i

2 ttitle 'Employee Annual Salaries|As of Nov-6-1997'

Hit enter twice to get back to the SQL> prompt.

LIST the file.

SQL>l

1 spool repfile

2 ttitle 'Employee Annual Salaries|As of Nov-6-1997'

3 select deptno, name, job, salary*12

4 from employee

5 order by deptno;

6 spool off

LIST line 6. Use the INPUT command to add the following line.

SQL>l6

6*Spool off

SQL>i

7 ttitle off

Hit enter twice.

Save your query file. Remember to end the command with replace.

SQL>save empreport replace

Run the file.

SQL> @empreport

Thu Nov 06 page 1

Employee Annual Salaries

As of Nov-6-1997

 

DEPTNO

NAME

JOB

SALARY*12

10

Wilson

Clrk

20400

10

Chen

Mngr

34800

30

Reed

Anlt

42000

30

Watson

Mngr

54000

40

Smith

Slsm

30000

40

Allen

Mngr

45600

40

Schwartz

Slsm

51000

50

Turner

Clrk

21600

50

Ramirez

Mngr

43800

60

McDonnel

Clrk

19500

60

Simpson

Drvr

9900

60

Di Salvo

Spvr

32400

 

Notice the current date and page number are placed on the report. The TTITLE command instructs SQL*Plus to place your title at the top of the page. Your title is what you typed within single quotes in the TTITLE command. The vertical line is the heading separator. It tells SQL*Plus that there should be a break wherever the | is placed. The vertical bar in our TTITLE command instructs SQL*Plus to place the text before the bar on one line and the text after the bar on the next line. Notice we included an instruction at the end of the file to turn TTITLE off.

Your report may contain breaks. Let's set the page size, linesize and newpage settings to ensure that our report fits on a page.

SQL> get empreport

1 spool repfile

2 ttitle 'Employee Annual Salaries|As of Nov-6-1997'

3 select deptno, name, job, salary*12

4 from employee

5 order by deptno;

6 ttitle off

7 spool off

LIST line 2, then use the INPUT command to add a new line.

SQL>l2

2* ttitle 'Employee Annual Salaries|As of Nov-6-1997'

SQL>i

3i set linesize 60

4i set pagesize 50

5i set newpage 0

Hit enter twice to return to the SQL> prompt.

Save the file and then run it.

SQL> save empreport replace

SQL>@empreport

You report should contain the same results, but the format may change so that there are no breaks in the information. To set linesize use the command SET LINESIZE number_of_characters. To set the number of lines on a page, use SET PAGESIZE number_of_lines. To set the number of lines between the beginning of each page and the top title, use SET NEWPAGE number_of_lines. If NEWPAGE is set to zero, SQL*Plus skips zero lines and displays and prints a formfeed character to begin a new page.

Now let's change the column headings on our report.

SQL> get empreport

1 spool repfile

2 ttitle 'Employee Annual Salaries|As of Nov-6-1997'

3 set linesize 60

4 set pagesize 50

5 set newpage 0

6 select deptno, name, job, salary*12

7 from employee

8 order by deptno;

9 ttitle off

10 spool off

LIST line 2. We want to add lines following it, so use the INPUT(i) command.

L> l2

2* ttitle 'Employee Annual Salaries|As of Nov-6-1997'

L> i

3i column deptno heading Department

4i column name heading 'Employee Name'

5i column job heading Position

6i column salary*12 heading 'Annual Salary'

After typing line 6, hit enter twice. Then LIST the file. LIST line 12. Use INPUT to add a new line.

SQL>l

1 spool repfile

2 ttitle 'Employee Annual Salaries|As of Nov-6-1997'

3 column deptno heading Department

4 column name heading 'Employee Name'

5 column job heading Position

6 column salary*12 heading 'Annual Salary'

7 set linesize 60

8 set pagesize 50

9 set newpage 0

10 select deptno, name, job, salary*12

11 from employee

12 order by deptno;

13 ttitle off

14 spool off

SQL>l12

12 order by deptno;

SQL>i

13i clear columns

Hit enter twice. Then save the new file. Remember you are replacing the old copy, so you need to type replace after the file name.

L> save empreport replace

Run the file.

L> @empreport

Thu Nov 06 page 1

Employee Annual Salaries

As of Nov-6-1997

DEPARTMENT

EMPLOYEE

POSI

ANNUAL SALARY

10

Wilson

Clrk

20400

10

Chen

Mngr

34800

30

Reed

Anlt

42000

30

Watson

Mngr

54000

40

Smith

Slsm

30000

40

Allen

Mngr

45600

40

Schwartz

Slsm

51000

50

Turner

Clrk

21600

50

Ramirez

Mngr

43800

60

McDonnel

Clrk

19500

60

Simpson

Drvr

9900

60

Di Salvo

Spvr

32400

We used the HEADING clause of the COLUMN command to define more useful column headings for our report. Notice that there are single quotation marks around the new headings that are two words. To change a column heading to two or more words, enclose the new heading in single quotation marks in the COLUMN command. To display a column heading on more than one line, use the vertical bar (|) where you want to begin a new line. Notice we included a command at the end to clear columns.

We have a problem with some of our new headings - they won't fit in the SQL*Plus default display width. We need to reformat the columns so that longer column titles will appear. We will use the line editing capabilities of SQL*Plus. Return to Error Correction in SQL*Plus if you need to review line editing.

SQL> get empreport

1 spool repfile

2 ttitle 'Employee Annual Salaries|As of Nov-6-1997'

3 column deptno heading Department

4 column name heading 'Employee Name'

5 column job heading Position

6 column salary*12 heading 'Annual Salary'

7 set linesize 60

8 set pagesize 50

9 set newpage 0

10 select deptno, name, job, salary*12

11 from employee

12 order by deptno;

13 clear columns

14 ttitle off

15 spool off

LIST line 4 so that we may change it.

SQL> l4

4* column name heading 'Employee Name'

Remember that c is the abbreviation for the CHANGE command. We put the old text after the first slash and the new text after the second slash.

SQL> c/name h/name format a15 h

4* column name format a15 heading 'Employee Name'

LIST line 5. Use the CHANGE command to make the following change.

SQL> l5

5* column job heading Position

SQL> c/job h/job format a12 h

5* column job format a12 heading Position

Save the new file. Then run it.

SQL> save empreport replace

SQL> @empreport

Thu Nov 06 page 1

Employee Annual Salaries

As of Nov-6-1997

DEPARTMENT

EMPLOYEE NAME

POSITION

ANNUAL SALARY

10

Wilson

Clrk

20400

10

Chen

Mngr

34800

30

Reed

Anlt

42000

30

Watson

Mngr

54000

40

Smith

Slsm

30000

40

Allen

Mngr

45600

40

Schwartz

Slsm

51000

50

Turner

Clrk

21600

50

Ramirez

Mngr

43800

60

McDonnel

Clrk

19500

60

Simpson

Drvr

9900

60

Di Salvo

Spvr

32400

The format of the column title may be changed using the FORMAT clause of the COLUMN command. Since both the NAME and JOB columns in the original table are defined as CHAR data types, we use the letter A followed by  a number representing the width of the column in characters. The EMPLOYEE NAME column will be 15 characters wide and the POSITION column will be 12 characters wide.

Now let's change the format of the salary column so that a dollar sign is placed in front of the salary, a comma is placed after the thousands, and two digits appear after the decimal.

SQL> get empreport

1 spool repfile

2 ttitle 'Employee Annual Salaries|As of Nov-6-1997'

3 column deptno heading Department

4 column name format a15 heading 'Employee Name'

5 column job format a12 heading Position

6 column salary*12 heading 'Annual Salary'

7 set linesize 60

8 set pagesize 50

9 set newpage 0

10 select deptno, name, job, salary*12

11 from employee

12 order by deptno;

13 clear columns

14 ttitle off

15 spool off

LIST line 6. Use the CHANGE command to edit the line.

SQL> l6

6* column salary*12 heading 'Annual Salary'

SQL> c/12 h/12 format $999,990.99 h

6* column salary*12 format $999,990.99 heading 'Annual Salary'

Save the new file.

SQL> save empreport replace

Run the file

SQL> @empreport

Thu Nov 06 page 1

Employee Annual Salaries

As of Nov-6-1997

DEPARTMENT

EMPLOYEE NAME

POSITION

ANNUAL SALARY

10

Wilson

Clrk

$20,400.00

10

Chen

Mngr

$34,800.00

30

Reed

Anlt

$42,000.00

30

Watson

Mngr

$54,000.00

40

Smith

Slsm

$30,000.00

40

Allen

Mngr

$45,600.00

40

Schwartz

Slsm

$51,000.00

50

Turner

Clrk

$21,600.00

50

Ramirez

Mngr

$43,800.00

60

McDonnel

Clrk

$19,500.00

60

Simpson

Drvr

$9,900.00

60

Di Salvo

Spvr

$32,400.00

The format of the salary column was specified as 8 digits with a decimal, a comma after the thousands and a leading $.

Next, let's insert a break after each department.

SQL> get empreport

1 spool repfile

2 ttitle 'Employee Annual Salaries|As of Nov-6-1997'

3 column deptno heading Department

4 column name format a15 heading 'Employee Name'

5 column job format a12 heading Position

6 column salary*12 heading 'Annual Salary'

7 set linesize 60

8 set pagesize 50

9 set newpage 0

10 select deptno, name, job, salary*12

11 from employee

12 order by deptno;

13 clear columns

14 ttitle off

15 spool off

LIST line 6. Use the INPUT command to add a line.

SQL>l6

6* column salary*12 heading 'Annual Salary'

SQL>i

7i break on deptno skip 2

Hit enter twice to return to the SQL> prompt. Save the file and run it.

SQL>save empreport replace

SQL>@empreport

BREAK ON is used to create meaningful subset of records in a report. The column specified in the BREAK ON command should be the same column included in your ORDER BY clause. SKIP 2 tells SQL*Plus to insert two blank lines at each break point. Let's compute a subtotal for each department.

SQL> get empreport

1 spool repfile

2 ttitle 'Employee Annual Salaries|As of Nov-6-1997'

3 column deptno heading Department

4 column name format a15 heading 'Employee Name'

5 column job format a12 heading Position

6 column salary*12 format $999,990.99 heading 'Annual Salary'

7 break on deptno skip 2

8 set linesize 60

9 set pagesize 50

10 set newpage 0

11 select deptno, name, job, salary*12

12 from employee

13 order by deptno;

14 clear columns

15 ttitle off

16 spool off

LIST line 7. We want to add a line, so use the INPUT command.

SQL> l7

7* break on deptno skip 2

SQL> i

8i compute sum of salary*12 on deptno

Hit enter twice here.

LIST the file.

SQL>l

1 spool repfile

2 ttitle 'Employee Annual Salaries|As of Nov-6-1997'

3 column deptno heading Department

4 column name format a15 heading 'Employee Name'

5 column job format a12 heading Position

6 column salary*12 format $999,990.99 heading 'Annual Salary'

7 break on deptno skip 2

8 compute sum of salary*12 on deptno

9 set linesize 60

10 set pagesize 50

11 set newpage 0

12 select deptno, name, job, salary*12

13 from employee

14 order by deptno;

15 clear columns

16 ttitle off

17 spool off

List line 15. Use the INPUT command to add lines.

SQL>l15

15* clear columns

SQL> I

16i clear breaks

17i clear computes

Hit enter twice. Save the file.

SQL> save empreport replace

Run the file.

SQL> @empreport

Thu Nov 06 page 1

Employee Annual Salaries

As of Nov-6-1997

DEPARTMENT

EMPLOYEE NAME

POSITION

ANNUAL SALARY

10

Wilson

Clrk

$20,400.00

10

Chen

Mngr

$34,800.00

Sum

 

 

$55,200.00

30

Reed

Anlt

$42,000.00

30

Watson

Mngr

$54,000.00

Sum

 

 

$96,000.00

40

Smith

Slsm

$30,000.00

40

Allen

Mngr

$45,600.00

40

Schwartz

Slsm

$51,000.00

Sum

 

 

$126,600.00

50

Turner

Clrk

$21,600.00

50

Ramirez

Mngr

$43,800.00

Sum

 

 

$65,400.00

60

McDonnel

Clrk

$19,500.00

60

Simpson

Drvr

$9,900.00

60

Di Salvo

Spvr

$32,400.00

Sum

 

 

$61,800.00

The BREAK ON and COMPUTE commands work in conjunction with one another. The totals computed by the COMPUTE SUM command will be for the sections specified by the BREAK ON command. NOTE: Every COMPUTE SUM command requires the use of a BREAK ON command to guide it. The BREAK ON command can be used without a COMPUTE SUM command to provide groupings where no totals are necessary.

We also included commands to clear breaks and computes. Once COMPUTE, BREAK ON, or COLUMN commands are issued they remain active until you clear them or leave SQL*Plus. Therefore it is wise to clear these settings before issuing new commands or the commands from previous reports will act on the current report producing unwanted results.

All we need to do to complete our report is to include a command that will calculate and print the summary total of annual salaries.

SQL>get empreport

1 spool repfile

2 ttitle 'Employee Annual Salaries|As of Nov-6-1997'

3 column deptno heading Department

4 column name format a15 heading 'Employee Name'

5 column job format a12 heading Position

6 column salary*12 format $999,990.99 heading 'Annual Salary'

7 break on deptno skip 2

8 compute sum of salary*12 on deptno

9 set linesize 60

10 set pagesize 50

11 set newpage 0

12 select deptno, name, job, salary*12

13 from employee

14 order by deptno;

15 clear columns

16 clear breaks

17 clear computes

18 ttitle off

19 spool off

LIST line 7. Use the CHANGE command to modify the line.

SQL>l7

7* break on deptno skip 2

SQL>c/deptno skip/deptno on report skip

7 break on deptno on report skip 2

LIST line 8. Use INSERT to add a line after line 8.

SQL>l8

8* compute sum of salary*12 on deptno

SQL>i

9I compute sum of salary*12 on report

Hit enter twice to return to the SQL> prompt. Save the file and then run it.

SQL>save empreport replace

SQL>@empreport

DEPARTMENT

EMPLOYEE NAME

POSITION

ANNUAL SALARY

10

Wilson

Clrk

$20,400.00

10

Chen

Mngr

$34,800.00

Sum

 

 

$55,200.00

30

Reed

Anlt

$42,000.00

30

Watson

Mngr

$54,000.00

Sum

 

 

$96,000.00

40

Smith

Slsm

$30,000.00

40

Allen

Mngr

$45,600.00

40

Schwartz

Slsm

$51,000.00

Sum

 

 

$126,600.00

50

Turner

Clrk

$21,600.00

50

Ramirez

Mngr

$43,800.00

Sum

 

 

$65,400.00

60

McDonnel

Clrk

$19,500.00

60

Simpson

Drvr

$9,900.00

60

Di Salvo

Spvr

$32,400.00

Sum

 

 

$61,800.00

Sum

 

 

$405,000.00

 

Now we have accomplished our goal of building a complex report step-by-step and saving the report to a file. You may now ftp the file from csis to the computer at which you are working. Use WS-FTP to connect to csis.kennesaw.edu. Enter your username and password. Find the file in your directory. Once a copy of the file is on your computer, you may edit it if needed and print it using Word or another editing program.

Last revised 11/18/1997 by J. Warren, Kennesaw State University

Department of Computer Science and Information Systems

Return to ORACLE/SQL Tutorial Contents Page | Return to Command Files