Command Files
Through SQL*Plus, you can store one or more commands in a file called a command file. After creating a command file, you can retrieve, edit, and run it. The INPUT command is used to enter SQL*Plus commands into the buffer, then you will save the buffer contents. Let's write some simple interactive commands, then we will progress to formatting query results.
The following command file will direct the user to supply a report title and store the input in the variable MYTITLE. The PROMPT and ACCEPT commands allow you to ask for and store a value from the user. First, you may want to clear the buffer.
SQL> CLEAR BUFFER
You should receive a buffer cleared message.
SQL> INPUT
PROMPT Enter a title up to 30 characters long.
ACCEPT MYTITLE PROMPT 'Title:'
TTITLE CENTER MYTITLE SKIP 2
SELECT DEPTNO, DEPTNAME, LOC
FROM DEPT
Press the enter key twice here to tell ORACLE you have finished entering commands. Then use the following command to save the report to a file named PROMPTDEPT.
SQL> SAVE PROMPTDEPT
You will receive a Created file promptdept message. Now, run the command file.
SQL> @PROMPTDEPT
Enter a title up to 30 characters long. ç This is the message that is displayed for the user.
Title: ç This is the prompt at the beginning of the line, waiting for user input.
Department Report as of 11/4/97 ç This is what I entered for the title of the report, followed by enter.
The following report is displayed.
Department Report as of 11/4/97
|
DEPTNO |
DEPTNAME |
LOC |
|
10 |
Accounting |
Dallas |
|
30 |
Research |
San Francisco |
|
40 |
Sales |
Boston |
|
50 |
Manufacturing |
Houston |
|
60 |
Shipping |
Houston |
The TTITLE command sets the top title for your report. CENTER specifies the position of the top title.
Before continuing, turn the TTITLE command you entered in the command file off as shown below:
SQL> TTITLE OFF
Let's change the format of this report by changing the column headings. First retrieve the file.
SQL> GET PROMPTDEPT
1 PROMPT Enter a title up to 30 characters long.
2 ACCEPT MYTITLE PROMPT 'Title:'
3 TTITLE CENTER MYTITLE SKIP 2
4 SELECT DEPTNO, DEPTNAME, LOC
5 FROM DEPT
We want to add lines after line 3 that change column headings, so we LIST line 3. Then type the INPUT command followed by enters, and ORACLE allows us to add as many lines as we need.
SQL> L3
3* TTITLE CENTER MYTITLE SKIP 2
SQL> INPUT
4i COLUMN DEPTNO HEADING Department
5i COLUMN DEPTNAME HEADING Name
6i COLUMN LOC HEADING Location
Hit the enter key twice to let ORACLE know you are finished adding lines. Now, LIST the command file.
SQL> LIST
1 PROMPT Enter a title up to 30 characters long.
2 ACCEPT MYTITLE PROMPT 'Title:'
3 TTITLE CENTER MYTITLE SKIP 2
4 COLUMN DEPTNO HEADING Department
5 COLUMN DEPTNAME HEADING Name
6 COLUMN LOC HEADING Location
7 SELECT DEPTNO, DEPTNAME, LOC
8* FROM DEPT
Now you are ready to write over the old file and replace it with this new version.
SQL> SAVE PROMPTDEPT REPLACE
You will receive a message that ORACLE wrote the file.
Run the command file. Enter any title you prefer for your report. I chose "Department Report as of 11/4/97".
SQL> @PROMPTDEPT
Enter a title up to 30 character long.
Title:
Department Report as of 11/4/97
This is what the report looks like now.
Department Report as of 11/4/97
|
Department |
Name |
Location |
|
10 |
Accounting |
Dallas |
|
30 |
Research |
San Francisco |
|
40 |
Sales |
Boston |
|
50 |
Manufacturing |
Houston |
|
60 |
Shipping |
Houston |
Don't forget to turn off the TTITLE command. You also need to clear columns.
SQL> CLEAR COLUMNS
SQL> TTITLE OFF
Let's try creating a query that asks the user for input. Suppose we want to allow the user to find the information about any employee in the EMPLOYEE table by entering the employee number. The following commands will do this. First, clear the buffer.
SQL> INPUT
1 PROMPT Enter a valid 3 digit employee number.
2 ACCEPT ENUMBER NUMBER
3 SELECT NAME, JOB, SALARY, DEPTNO
4 FROM EMPLOYEE
5 WHERE EMPNO = &ENUMBER
Hit enter twice to let ORACLE know you have finished and then save the command file.
SQL> SAVE FINDEMPNO
Run the file.
SQL> @FINDEMPNO
Enter a valid 3 digit employee number. ç This message is displayed to prompt the user for input.
101 ç I chose to enter 101. You may use any employee number in the EMPLOYEE table.
ORACLE shows the old and new values of empno, then displays the report.
old 3: where empno = &enumber
new 3: where empno = 101
|
NAME |
JOB |
SALARY |
DEPTNO |
|
Smith |
Slsm |
2500 |
40 |
Since we specified that our variable was a number, other values are not accepted. Try entering a value that is not a number.
SQL> @FINDEMPNO
Enter a valid 3 digit employee number.
John ç I entered "John".
"John" is not a valid number ç ORACLE tells me my input is not valid.
100 ç I try again and enter a number this time. The following report is displayed.
old 3: where empno = &enumber
new 3: where empno = 100
|
NAME |
JOB |
SALARY |
DEPTNO |
|
Wilson |
Clrk |
1700 |
10 |
Let's change this query so that the salary is formatted with a dollar sign, and with a comma after the thousands. Retrieve the command file.
SQL> GET FINDEMPNO
1 PROMPT Enter a valid 3 digit employee number.
2 ACCEPT ENUMBER NUMBER
3 SELECT NAME, JOB, SALARY, DEPTNO
4 FROM EMPLOYEE
5* WHERE EMPNO = &ENUMBER
We want to add a line after line 2, so LIST line 2, then use the INPUT command.
SQL> L2
2* ACCEPT ENUMBER NUMBER
SQL> INPUT
3i COLUMN SALARY FORMAT $99,990
Hit enter twice to show ORACLE you are finished adding lines. Then LIST the new command.
SQL> LIST
1 PROMPT Enter a valid 3 digit employee number.
2 ACCEPT ENUMBER NUMBER
3 COLUMN SALARY FORMAT $99,990
4 SELECT NAME, JOB, SALARY, DEPTNO
5 FROM EMPLOYEE
6* WHERE EMPNO = &ENUMBER
Save the changed file, then run it.
SQL> SAVE FINDEMPNO REPLACE
SQL> @FINDEMPNO
Enter a valid 3 digit employee number.
101
old 3: where empno = &enumber
new 3: where empno = 101
|
NAME |
JOB |
SALARY |
DEPTNO |
|
Smith |
Slsm |
$2,500 |
40 |
Clear columns before continuing.
SQL> CLEAR COLUMNS
Last revised 11/5/1997 by J. Warren, Kennesaw State University
Department of Computer Science and Information Systems
![]()
Return to ORACLE/SQL Contents Page | Return to SQL*Plus Commands | Go to Reports With SQL*Plus