Interacting with SQL*Plus

This section covers essential information that you need to know in order to interact with SQL*Plus. Here you will learn how to start SQL*Plus, enter commands, delimit strings, and name variables.

Starting SQL*Plus

SQL*Plus is almost always invoked by issuing the sqlplus command from your operating system command prompt. On Microsoft Windows systems, you also have the option of selecting an icon from the Start menu. Early releases of SQL*Plus on Windows used executable names such as PLUS33 and PLUS80W depending on the specific release number and on whether the DOS version or the Windows version was to be invoked.

Syntax for the sqlplus command

The syntax used to invoke SQL*Plus looks like this:

sqlplus [[-S[ILENT]] [-R[ESTRICT] level]
   [-M[ARKUP] markup_options]
   [username[/password][@connect]|/|/NOLOG]
   [@scriptfile [arg1 arg2 arg3...]]] | - | -?

The -RESTRICT and -MARKUP parameters are new in release 8.1.6. The descriptions of the parameters follow:

-S[ILENT]

Tells SQL*Plus to run in silent mode. No startup messages will be displayed; no command prompt will be displayed; no commands will be echoed to the screen.

-R[ESTRICT] level

Restricts what the user can do from SQL*Plus. The level must be one of the following:

1

Disables the EDIT, HOST, and ! commands.

2

Disables the EDIT, HOST, !, SAVE, SPOOL, and STORE commands.

3

Disables the EDIT, GET, HOST, !, SAVE, START, @, @@, SPOOL, and STORE commands.

Level 3 also disables the reading of the login.sql file. The glogin.sql file will be read, but restricted commands won’t be executed.

-M[ARKUP] markup_options

Allows you to specify the markup language to use when generating output. Except for HTML, all markup options are optional. The following are valid markup options:

HTML [ON | OFF]

Specifies the markup language to use and enables or disables the use of that markup language. In release 8.1.6, this is a mandatory option.

HEAD text

Specifies content for the <head> tag. The tag ends up being written as <head>text < /head>.

BODY text

Specifies content for the <body> tag. The tag ends up being written as <body text >.

ENTMAP {ON | OFF}

Controls whether SQL*Plus uses HTML equivalents such as &lt and &gt for special characters.

SPOOL {ON | OFF}

Controls whether SQL*Plus writes to the spool file using plain text or the specified markup language (currently HTML).

PRE[FORMAT] {ON | OFF}

Controls whether spooled report output is enclosed within <pre> ... </pre> tags.

On some operating systems, you need to enclose the entire string of markup options within double quotes.

username [ /password ][ @connect ]

Is your database login information.

/

Connects you to a local database using operating-system authentication.

/NOLOG

Tells SQL*Plus to start without connecting to a database first.

scriptfile

Is the name of a SQL*Plus script file. SQL*Plus will start up, execute the file, and then exit.

arg1 agr2 arg3

Are optional command-line arguments to pass to your script. Separate arguments by at least one space.

-

Causes SQL*Plus to display a short summary of this syntax.

- ?

Causes SQL*Plus to display version and copyright information.

Entering Commands

How you enter commands in SQL*Plus depends a bit on whether you are entering a command to SQL*Plus itself or are entering a SQL statement or a PL/SQL block.

Entering SQL*Plus commands

Commands such as DESCRIBE, COLUMN, TTITLE, SET, and all the others listed in the Section 1.7 section are commands to SQL*Plus itself. These must be entered on one line and are executed immediately after you enter them. For example:

SET ECHO ON
DESCRIBE employee

SQL*Plus commands may optionally be terminated by a semicolon. For example:

PROMPT This semicolon won't print.;
CONNECT system/manager;

You can change this behavior of SQL*Plus towards semi-colons by changing the SQLTERMINATOR setting.

Long SQL*Plus commands may be continued onto multiple physical lines. The SQL*Plus continuation character is a hyphen (-). Use it at the end of a physical line to continue a long SQL*Plus command to the next line. The following three lines, for example, are treated as one by SQL*Plus:

COLUMN employee_id -
FORMAT 099999 -
HEADING 'Emp ID'

The space in front of the continuation character is optional. Quote strings may also be continued. For example:

SELECT 'Hello-
World!' FROM dual;

When you are continuing a quoted string, any spaces before the continuation character will be included in the string. The line break also counts as one space.

Entering SQL statements

SQL statements may span multiple lines and must always be terminated. This may be done using either a semi-colon ( ; ) or a forward slash ( / ). For example:

SELECT user
FROM dual;
SELECT user
FROM dual
/

In both of these cases, the SQL statement will be entered into a buffer known as the SQL buffer and then will be executed. You may also terminate a SQL statement using either a blank line or a period, in which case the statement is stored in the buffer but not executed. For example:

SQL> SELECT user
  2  FROM dual
  3  
SQL> SELECT user
  2  FROM dual
  3  .

Use the SET SQLTERMINATOR command to change the terminator from a semicolon to some other character. Use SET SQLBLANKLINES ON to allow blank lines within a SQL statement. To execute the statement currently in the buffer, enter a forward slash on a line by itself.

Entering PL /SQL blocks

PL/SQL blocks may span multiple lines and may contain blank lines. They must be terminated by either a forward slash or a period (.) on a line by itself. For example:

BEGIN
   DBMS_OUTPUT.PUT_LINE('Hello World!'),
END;
/

BEGIN
   DBMS_OUTPUT.PUT_LINE('Hello World!'),
END;
.

When a forward slash is used, the block is sent to the server and executed immediately. When a period is used, the block is only stored in the SQL buffer. Use the SET BLOCKTERMINATOR command to change the block terminator from a period to some other character.

Strings in SQL*Plus Commands

Many SQL*Plus-specific commands take string values as parameters. Simple strings containing no spaces or punctuation characters may be entered without quotes. Here’s an example:

COLUMN employee_id HEADING emp_id

Generally, it’s safer to use quoted strings. Either single or double quotes may be used. For example:

COLUMN employee_id HEADING 'Emp #'
COLUMN employee_id HEADING "Emp #"

To embed quotes in a string, either double them or use a different enclosing quote. The following two commands have equivalent results:

COLUMN employee_id HEADING '''Emp #'''
COLUMN employee_id HEADING "'Emp #'"

The single exception to these rules is the PROMPT command. All quotes used in a PROMPT command will appear in the output.

Specifying Filenames

Several SQL*Plus commands allow you to specify a filename. In all cases, you may also include a path and/or an extension with the name. For example:

SPOOL my_report
SPOOL c:	empmy_report
SPOOL create_synonyms.sql

Most file-related commands assume a default extension if you don’t supply one. The default varies by command.

Naming Variables

SQL*Plus allows you to declare two types of variables: user variables and bind variables. The rules for naming each type are different.

User variable names may contain letters, digits, and underscores ( _ ) in any order. They are case-insensitive and are limited to 30 characters in length.

Bind variable names must begin with a letter, but after that may contain letters, digits, underscores, dollar signs ($), and number signs (#). They also are case-insensitive and are limited to 30 characters in length.

Using column aliases

If a SELECT statement includes columns that are expressions, Oracle will generate a column name based on the expression. Take a look at the following SQL statement:

SELECT SUM(hours_logged)
FROM project_hours
WHERE project_id = 1001;

The name of the column returned by this query will be SUM(HOURS_LOGGED). That means that any COLUMN commands used to format the output will need to look like this:

COLUMN SUM(HOURS_LOGGED) -
HEADING 'Total Hours'

As your expressions become more complicated, the Oracle-generated names become difficult to deal with. It’s better to use a column alias to supply a more user-friendly name for the computed column. For example:

SELECT SUM(hours_logged) total_hours
FROM project_hours
WHERE project_id = 1001;

Now the column name is obvious. It’s total_hours, and it won’t change even if the expression changes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset