SQL*Plus User's Guide and Reference

Contents Index Home Previous Next

Entering and Executing Commands

Entering Commands

Your computer's cursor, or pointer (typically an underline, a rectangular block, or a slash), appears after the command prompt. The cursor indicates the place where the next character you type will appear on your screen.

To tell SQL*Plus what to do, simply type the command you wish to enter. Usually, you separate the words in a command from each other by a space or tab. You can use additional spaces or tabs between words, if you wish, to make your commands more readable.

Note: You will see examples of spacing and indentation throughout this Guide. When you enter the commands in the exercises, you do not have to space them as shown, but you may find them clearer to read if you do.

You can enter commands in capitals or lowercase. For the sake of clarity, all table names, column names, and commands in this Guide appear in capital letters.

You can enter three kinds of commands at the command prompt:

The manner in which you continue a command on additional lines, end a command, or execute a command differs depending on the type of command you wish to enter and run. Examples of how to run and execute these types of commands are found later in this chapter.

Getting Help

To get online help for SQL*PLUS commands, type HELP at the command prompt followed by the name of the command. For example:

SQL>HELP ACCEPT

If you get a response indicating that help is not available, consult your database administrator. For more details about the help system, see the HELP command[*].

Executing Commands

After you enter the command and direct SQL*Plus to execute it, SQL*Plus processes the command and redisplays the command prompt, indicating that you can enter another command.

Running SQL Commands

The SQL command language enables you to manipulate data in the database. See your Oracle7 Server SQL Language Reference Manual for information on individual SQL commands.

Example 2-3 Entering a SQL Command

In this example, you will enter and execute a SQL command to display the employee number, name, job, and salary of each employee in the sample table EMP.

	SQL> SELECT EMPNO, ENAME, JOB, SAL

	2  FROM EMP WHERE SAL < 2500;

	     EMPNO ENAME        JOB               SAL
	---------- ------------ ---------- ----------
	      7369 SMITH        CLERK             800
	      7499 ALLEN        SALESMAN         1600
	      7521 WARD         SALESMAN         1250
	      7654 MARTIN       SALESMAN         1250
	      7782 CLARK        MANAGER          2450
	      7844 TURNER       SALESMAN         1500
	      7876 ADAMS        CLERK            1100
	      7900 JAMES        CLERK             800
	      7934 MILLER       CLERK            1300

	9 rows selected
	SQL>

Understanding SQL Command Syntax

Just as spoken language has syntax rules that govern the way we assemble words into sentences, SQL*Plus has syntax rules that govern how you assemble words into commands. You must follow these rules if you want SQL*Plus to accept and execute your commands.

Dividing a SQL Command into Separate Lines You can divide your SQL command into separate lines at any points you wish, as long as individual words are not split between lines. Thus, you can enter the query you entered in Example 2-3 on one line:

SQL> SELECT EMPNO, ENAME, JOB, SAL FROM EMP WHERE SAL < 2500;

You can also enter the query on several lines:

SQL> SELECT
  2  EMPNO, ENAME, JOB, SAL
  3  FROM EMP
  4  WHERE SAL < 2500;

In this Guide, you will find most SQL commands divided into clauses, one clause on each line. In Example 2-3, for instance, the SELECT and FROM clauses were placed on separate lines. Many people find this most convenient, but you may choose whatever line division makes your command most readable to you.

Ending a SQL Command You can end a SQL command in one of three ways:

A semicolon (;) tells SQL*Plus that you want to run the command. Type the semicolon at the end of the last line of the command, as shown in Example 2-3, and press [Return]. SQL*Plus will process the command and store it in the SQL buffer (see "The SQL Buffer" below for details). If you mistakenly press [Return] before typing the semicolon, SQL*Plus will prompt you with a line number for the next line of your command. Type the semicolon and press [Return] again to run the command.

Note: You cannot enter a comment (/* */) on the same line on which you enter a semicolon.

A slash (/) on a line by itself also tells SQL*Plus that you wish to run the command. Press [Return] at the end of the last line of the command. SQL*Plus prompts you with another line number. Type a slash and press [Return] again. SQL*Plus will execute the command and store it in the buffer (see "The SQL Buffer" below for details).

A blank line tells SQL*Plus that you have finished entering the command, but do not want to run it yet. Press [Return] at the end of the last line of the command. SQL*Plus prompts you with another line number.

Press [Return] again; SQL*Plus now prompts you with the SQL*Plus command prompt. SQL*Plus does not execute the command, but stores it in the SQL buffer (see "The SQL Buffer" below for details). If you subsequently enter another SQL command, SQL*Plus overwrites the previous command in the buffer.

Creating Stored Procedures Stored procedures are PL/SQL functions, packages, or procedures. To create stored procedures, you use SQL CREATE commands. The following SQL CREATE commands are used to create stored procedures:

Entering any of these commands places you in PL/SQL mode, where you can enter your PL/SQL subprogram (see also "Running PL/SQL Blocks" in this chapter). When you are done typing your PL/SQL subprogram, enter a period (.) on a line by itself to terminate PL/SQL mode. To run the SQL command and create the stored procedure, you must enter RUN or slash (/). A semicolon (;) will not execute these CREATE commands.

When you use CREATE to create a stored procedure, a message appears if there are compilation errors. To view these errors, you use SHOW ERRORS. For example:

SQL> SHOW ERRORS PROCEDURE ASSIGNVL

See Chapter 6 for a description of the SHOW command.

To execute a PL/SQL statement that references a stored procedure, you can use the EXECUTE command. EXECUTE runs the PL/SQL statement that you enter immediately after the command. For example:

SQL> EXECUTE :ID := EMP_MANAGEMENT.GET_ID('BLAKE')

See Chapter 6 for a description of the EXECUTE command.

The SQL Buffer

The area where SQL*Plus stores your most recently entered SQL command or PL/SQL block is called the SQL buffer. The command or block remains there until you enter another. Thus, if you want to edit or rerun the current SQL command or PL/SQL block, you may do so without re-entering it. See Chapter 3 for details about editing or rerunning a command or block stored in the buffer.

SQL*Plus does not store the semicolon or the slash you type to execute a command in the SQL buffer.

Note: SQL*Plus commands are not stored in the SQL buffer.

Executing the Current SQL Command or PL/SQL Block from the Command Prompt

You can run (or rerun) the current SQL command or PL/SQL block by entering the RUN command or the slash (/) command at the command prompt. The RUN command lists the SQL command or PL/SQL block in the buffer before executing the command or block; the slash (/) command simply runs the SQL command or PL/SQL block.

Running PL/SQL Blocks

You can also use PL/SQL subprograms (called blocks) to manipulate data in the database. See your PL/SQL User's Guide and Reference for information on individual PL/SQL statements.

To enter a PL/SQL subprogram in SQL*Plus, you need to be in PL/SQL mode. You are placed in PL/SQL mode when

SQL*Plus treats PL/SQL subprograms in the same manner as SQL commands, except that a semicolon (;) or a blank line does not terminate and execute a block. Terminate PL/SQL subprograms by entering a period (.) by itself on a new line.

SQL*Plus stores the subprograms you enter at the SQL*Plus command prompt in the SQL buffer. Execute the current subprogram by issuing a RUN or slash (/) command. Likewise, to execute a SQL CREATE command that creates a stored procedure, you must also enter RUN or slash (/). A semicolon (;) will not execute these SQL commands as it does other SQL commands.

SQL*Plus sends the complete PL/SQL subprogram to Oracle for processing (as it does SQL commands). See your PL/SQL User's Guide and Reference for more information.

You might enter and execute a PL/SQL subprogram as follows:

SQL> DECLARE
  2      x   NUMBER := 100;
  3  BEGIN
  4      FOR i IN 1..10 LOOP
  5          IF MOD (i, 2) = 0 THEN    --i is even
  6            INSERT INTO temp VALUES (i, x, 'i is even');
  7          ELSE
  8            INSERT INTO temp VALUES (i, x, 'i is odd');
  9          END IF;
 10          x := x + 100;
 11      END LOOP;
 12  END;
 13  .
SQL> /

PL/SQL procedure successfully completed.

When you run a subprogram, the SQL commands within the subprogram may behave somewhat differently than they would outside of the subprogram. See your PL/SQL User's Guide and Reference for detailed information on the PL/SQL language.

Running SQL*Plus Commands

You can use SQL*Plus commands to manipulate SQL commands and PL/SQL blocks and to format and print query results. SQL*Plus treats SQL*Plus commands differently than SQL commands or PL/SQL blocks. For information on individual SQL*Plus commands, refer to the following chapters of this Guide.

To speed up command entry, you can abbreviate many SQL*Plus commands to one or a few letters. Abbreviations for some SQL*Plus commands are described along with the commands in Chapters 3, 4, and 5. For abbreviations of all SQL*Plus commands, refer to the command descriptions[*].

Example 2-4 Entering a SQL*Plus Command

This example shows how you might enter a SQL*Plus command to change the format used to display the column SAL of the sample table EMP.

	SQL> COLUMN SAL FORMAT $99,999 HEADING SALARY

	SQL> RUN
	1  SELECT EMPNO, ENAME, JOB, SAL
	2* FROM EMP WHERE SAL < 2500
	   EMPNO ENAME         JOB          SALARY
	-------- ------------- ---------- --------
	    7369 SMITH         CLERK          $800
	    7499 ALLEN         SALESMAN     $1,600
	    7521 WARD          SALESMAN     $1,250
	    7654 MARTIN        SALESMAN     $1,250
	    7782 CLARK         MANAGER      $2,450
	    7844 TURNER        SALESMAN     $1,500
	    7876 ADAMS         CLERK        $1,100
	    7900 JAMES         CLERK          $800
	    7934 MILLER        CLERK        $1,300

The COLUMN command formatted the column SAL with a dollar sign ($) and a comma (,) and gave it a new heading. The RUN command then reran the query of Example 2-3, which was stored in the buffer. SQL*Plus does not store SQL*Plus commands in the SQL buffer.

Understanding SQL*Plus Command Syntax

SQL*Plus commands have a different syntax from SQL commands or PL/SQL blocks.

Continuing a Long SQL*Plus Command on Additional Lines You can continue a long SQL*Plus command by typing a hyphen at the end of the line and pressing [Return]. If you wish, you can type a space before typing the hyphen. SQL*Plus displays a right angle-bracket (>) as a prompt for each additional line. For example:

SQL> COLUMN SAL FORMAT $99,999 -
>  HEADING SALARY

Ending a SQL*Plus Command You do not need to end a SQL*Plus command with a semicolon. When you finish entering the command, you can just press [Return]. If you wish, however, you can enter a semicolon at the end of a SQL*Plus command.

Variables that Affect Running Commands

The SQL*Plus command SET controls many variables--called system variables--the settings of which affect the way SQL*Plus runs your commands. System variables control a variety of conditions within SQL*Plus, including default column widths for your output, whether SQL*Plus displays the number of records selected by a command, and your page size. System variables are also called SET command variables.

The examples in this Guide are based on running SQL*Plus with the system variables at their default settings. Depending on the settings of your system variables, your output may appear slightly different than the output shown in the examples. (Your settings might differ from the default settings if you have a SQL*Plus LOGIN file on your computer.)

For more information on system variables and their default settings, see the SET command[*]. For details on the SQL*Plus LOGIN file, refer to the section "Setting Up Your SQL*Plus Environment" under "Saving Commands for Later Use"[*] and to the SQLPLUS command[*].

To list the current setting of a SET command variable, enter SHOW followed by the variable name at the command prompt. See the SHOW command[*] for information on other items you can list with SHOW.

Saving Changes to the Database Automatically

Through the SQL DML commands UPDATE, INSERT, and DELETE--which can be used independently or within a PL/SQL block--specify changes you wish to make to the information stored in the database. These changes are not made permanent until you enter a SQL COMMIT command or a SQL DCL or DDL command (such as CREATE TABLE), or use the autocommit feature. The SQL*Plus autocommit feature causes pending changes to be committed after a specified number of successful SQL DML transactions. (A SQL DML transaction is either an UPDATE, INSERT, or DELETE command, or a PL/SQL block.)

You control the autocommit feature with the SQL*Plus SET command's AUTOCOMMIT variable. It has these forms:

SET AUTOCOMMIT ON Turns autocommit on.
SET AUTOCOMMIT OFF Turns autocommit off (the default).
SET AUTOCOMMIT n Commits changes after n SQL commands or PL/SQL blocks.
Example 2-5 Turning Autocommit On

To turn the autocommit feature on, enter

SQL> SET AUTOCOMMIT ON

Until you change the setting of AUTOCOMMIT, SQL*Plus will automatically commit changes from each SQL command or PL/SQL block that specifies changes to the database. After each autocommit, SQL*Plus displays the following message:

commit complete

When the autocommit feature is turned on, you cannot roll back changes to the database.

To commit changes to the database after a number of SQL DML commands or PL/SQL blocks, for example, ten, enter

SQL> SET AUTOCOMMIT 10

SQL*Plus counts SQL DML commands and PL/SQL blocks as they are executed and commits the changes after the tenth SQL DML command or PL/SQL block.

Note: For this feature, a PL/SQL block is regarded as one transaction, regardless of the actual number of SQL commands contained within it.

To turn the autocommit feature off again, enter the following command:

SQL> SET AUTOCOMMIT OFF

To confirm that AUTOCOMMIT is now set to OFF, enter the following SHOW command:

SQL> SHOW AUTOCOMMIT
autocommit OFF

For more information, see the AUTOCOMMIT variable of the SET command[*].

Stopping a Command while It Is Running

Suppose you have displayed the first page of a 50 page report and decide you do not need to see the rest of it. Press [Cancel]. (Refer to Table 2 - 1 at the beginning of this chapter to see how [Cancel] is labelled on your keyboard.) SQL*Plus will stop the display and return to the command prompt.

Note: Pressing [Cancel] will not stop the printing of a file that you have sent to a printer with the OUT clause of the SQL*Plus SPOOL command. (You will learn about printing query results[*].) You can stop the printing of a file through your operating system; see your operating system manuals for information.

Collecting Timing Statistics on Commands You Run

Use the SQL*Plus command TIMING to collect and display data on the amount of computer resources used to run one or more commands or blocks. TIMING collects data for an elapsed period of time, saving the data on commands run during the period in a timer. See TIMING[*] and the Oracle installation and user's manuals provided for your operating system for more information.

To delete all timers, enter CLEAR TIMING at the command prompt.

Running Host Operating System Commands

You can execute a host operating system command from the SQL*Plus command prompt. This is useful when you want to perform a task such as listing existing host operating system files.

To run a host operating system command, enter the SQL*Plus command HOST followed by the host operating system command. For example, this SQL*Plus command runs a host command, DIRECTORY *.SQL:

SQL> HOST DIRECTORY *.SQL

When the host command finishes running, the SQL*Plus command prompt appears again.

Running SQL*Forms Forms

If the RUNFORM option was enabled during SQL*Plus installation, you can also run a SQL*Forms form from the SQL*Plus command prompt. To run a form, enter the SQL*Plus command RUNFORM followed by the form name:

SQL> RUNFORM myform


Contents Index Home Previous Next