In this chapter: Starting SQL*Plus Some Basic SQL*Plus Commands Running SQL Queries Working with PL/SQL The SQL Buffer Line Editing Executing the Statement in the Buffer Saving an
Trang 12
Interacting with SQL*Plus
In this chapter:
Starting SQL*Plus
Some Basic SQL*Plus Commands
Running SQL Queries
Working with PL/SQL
The SQL Buffer
Line Editing
Executing the Statement in the Buffer
Saving and Retrieving the Buffer
The EDIT Command
Executing a Script
The Working Directory
This chapter shows you the rudiments of interacting with SQL*Plus The first part of this chapter shows how to start SQL*Plus and connect to a database You will then read about a few basic SQL*Plus commands you can use to get started Following that are some examples showing how to enter SQL statements and PL/SQL blocks The remainder of the chapter focuses on editing your SQL and PL/SQL There is extensive coverage of the built-in line-editing
commands, and you will also see how you can work with an external editor such as Windows' Notepad
Starting SQL*Plus
How you start SQL*Plus varies somewhat between operating systems, and also depends on whether you are operating
in a GUI environment such as Windows NT or a command-line environment such as DOS or Unix Generally speaking though, if you are using a command-line operating system, you use the SQLPLUS command On case-sensitive
operating systems, such as Unix, the command must be entered in lowercase as sqlplus If you are using Windows, or
another GUIbased operating system, you click or double-click on an icon
Command-Line Versus GUI Versions
There are two variations of SQL*Plus, command-line and GUI The command-line implementation was the original, and is the one shipped with Oracle for Unix, VMS, and other command-line operating systems A command-line
version even
Trang 2
< previous page page_19 next page >
Page 19
ships with the Windows client software, in case you want to invoke SQL*Plus from a DOS prompt For Microsoft Windows, Oracle has also developed a GUI implementation, which wraps the SQL*Plus command-line environment in
a standard window complete with scrollbars, drop-down menus, a close box, and other GUI adornments
At first glance, the GUI version doesn't appear to add anything extra, because it only implements a simple, scrolling window into which you type commands But there are some advantages to using it The GUI version implements cut and paste functionality, allows you to size the window any way you want, and implements a scrollback buffer so you never have to worry about your query results scrolling off the top of the screen If you are running under Windows, I highly recommend using the GUI version Once you get used to using SQL*Plus with a 1000-line scrollback buffer and
a large vertical window size, you won't want to go back
Operating System Authentication
Before talking about how to start SQL*Plus, it's important to understand how you authenticate yourself to Oracle in order to connect to a database There are two options here:
1 You log in with a database-specific username and password
2 Oracle authenticates you based on your operating system username, trusting that the operating system has properly
authenticated you This is known as operating system authentication.
Not all environments support operating system authentication, but in many cases the DBA is free to choose that as an option when creating a user The logonrelated examples in this chapter focus on the first situation, where you need a specific Oracle username/password, because that is most commonly the case There are notes, where appropriate, to explain how to connect to your database if you are using operating system authentication
Starting the GUI Version
To start the GUI version, first find the icon Figure 2-1 shows the SQL*Plus icon for SQL*Plus version 8 It looks like a blue disk drive topped with a yellow plus sign
Figure 2-1 The SQL*Plus icon
Trang 3You'll find the SQL*Plus icon in a program group under the Start menu On Windows 95, this group is named Oracle for Windows 95 Look at Figure 2-2 to see the Windows 95 Start menu expanded to show the SQL*Plus icon
Figure 2-2
Windows 95 Start menu expanded to show the SQL*Plus icon The Windows NT implementation is the same, except that the program group is named Oracle for Windows NT Select the SQL*Plus entry from the Start menu, and SQL*Plus will start
Logging into a database
After starting SQL*Plus, your next step is to log into the database Simply enter your database username, password, and host string into the dialog box and click OK Figure 2-3 shows how this will look
The host string tells SQL*Plus which database to connect to, and is sometimes referred to as a connect string In Figure 2-3, the host string is dev, probably indicating a development database Your DBA should give you the correct host string to use if you are connecting to a remote database You don't need one if you are connecting to the default
database on your local machine
Trang 4
< previous page page_21 next page >
Page 21
Figure 2-3
SQL*Plus logon dialog box
Be very careful to enter the correct information in the logon dialog box If you make a mistake, SQL*Plus will reprompt you for your username and password, but not for the connect string This is annoying when you are trying
to connect to a remote database See the later section, Making a mistake on the logon dialog
After you have successfully logged into your database, the SQL*Plus screen will look much like that shown in Figure 2-4
Trang 5Notice that SQL*Plus displays some potentially useful information for you now that you have connected to the
database The first few lines of the display show the
Trang 6
< previous page page_22 next page >
Page 22
specific SQL*Plus version and the copyright information After that, you are given some information about the database
to which you just connected SQL*Plus will display the specific database version, list any options that may be installed, and then show the specific PL/SQL version that is supported
From here you can go on to enter commands, run SQL queries, execute PL/SQL blocks, generate reports, run script files, and generally do anything else you might need to do with your database
Making a mistake on the logon dialog
One mildly annoying thing about the GUI version of SQL*Plus is the way it reacts if you make a mistake typing your username, password, and connect string into the logon dialog You would expect it to give you an error message and then redisplay the logon dialog so you can enter the correct information, but SQL*Plus does not do that Instead, when you make a mistake, SQL*Plus uses line-mode prompts to ask you for the correct information Figure 2-5 shows how this looks
Figure 2-5
The SQL*Plus screen after a logon failure The annoying thing is that at this point you will only be prompted for a username and password, not the connect string This is okay if you want to connect to your default database, because you don't need a connect string for that But if you want to connect to a remote database, you have a problem.The most obvious solution is to exit SQL*Plus (use File→
Exit), restart it, and try logging in again
Beginning with SQL*Plus version 8.0.4, Oracle has corrected this behavior If you make a mistake on the login, you will get the dialog box again
Trang 7
The other solution to this problem is to enter your username, password, and connect string all in response to the
username prompt For example:
Enter user-name: jonathan/mypass@dev
Oracle will parse out all three pieces of information, and you won't need to restart SQL*Plus in order to get the logon dialog back If you don't want your password to display, enter your username first, and the rest of the information at the password prompt Here's how that would look:
Enter user-name: jonathan
Enter password: mypass@dev
The ability to respond to a logon prompt with multiple pieces of information is not documented well in the Oracle manuals, but it sure does come in handy The rule is that at any prompt, you can also supply all the fields that
subsequent prompts would ask for
Missing the Product User Profile
SQL*Plus implements a security feature known as the product user profile Sometimes database administrators neglect
to create the tables and views that support this feature Consequently, SQL*Plus users will occasionally see an error message such as the following when connecting to a database:
Error accessing PRODUCT_USER_PROFILE
Warning: Proudct user profile information not loaded!
You may need to run PUPBLD.SQL as SYSTEM
Alarming as it sounds, this message is nothing to worry about Nothing is wrong with your database, and all SQL*Plus commands will work as normal This is only telling you that the product user profile security feature has not been
implemented at your site Chapter 9, The Product User Profile, describes this feature in detail, and also shows how your
database administrator can easily create the necessary tables to make this message go away If you don't know who your database administrator is, or you can't persuade him to create the profile table, don't worry about it Just ignore the message, and do whatever you need to do
Starting the Command-Line Version
To start the command-line version, you simply use the SQLPLUS command The following example shows how this
looks from a DOS command prompt:
C:\>SQLPLUS
SQL*Plus: Release 8.0.3.0.0 - Production on Sun Jan 25 10:12:55 1988
(c) Copyright 1997 Oracle Corporation All rights reserved
Enter user-name:
SQL*Plus will start by prompting you for a username and password
Trang 8
< previous page page_24 next page >
Page 24
You may need to use a command like PLUS80 or PLUS23, where the
last two digits represent the specific version of Oracle client software you have installed
The above command is fine if you want to connect to the default Oracle database or if you want to be prompted for your username and password To connect to a remote database, you must supply a connect string as part of your login One way
is to put your login information on the command line, as shown in this example:
C:\ORAWIN95\BIN>PLUS80 system/munising@oak
SQL*Plus: Release 8.0.4.0.0 - Production on Sat Oct 31 21:36:14 1998
(c) Copyright 1997 Oracle Corporation All rights reserved
Connected to:
Oracle7 Servser Release 7.3.4.0.0 - Production
With the distributed, replication, parallel query and Spatial Data options
PL/SQL Release 2.3.4.0.0 - Production
SQL>
The two examples just shown represent the most common ways to start SQL*Plus, especially if you're going to be using it interactively, as you will be for the examples in this chapter There are some other options and variations on the SQL*Plus command which are useful when you are executing SQL*Plus script files These are described next
Command-Line Options
Regardless of whether you are starting the GUI or the command-line version of SQL*Plus, there are some options you may want to be aware of You've already seen how you can pass your username and password on the command line In addition
to that, there are options to start SQL*Plus without connecting to a database, to start it in a silent mode, and to invoke a
script file
The option to start SQL*Plus without connecting to a database becomes especially important beginning with version 8.1, which ships with Oracle8i That version of SQL*Plus implements several administrative commands, such as STARTUP and SHUTDOWN, that used to be available only in Server Manager
Using these commands requires you to connect in an administrative mode, such as SYSDBA, and you can't do that from the command line See Chapter 10,
Administration with SQL*Plus, for more information on this.
Trang 9
The complete syntax for the SQLPLUS command is as follows:
SQLPLUS [[-SILENT] [username[/password][@connect] ¦/¦/NOLOG]
[@scriptfile [arg1 arg2 arg3 ]]]¦-¦-?
where:
SQLPLUS
Is the command to use when invoking SQL*Plus On a Unix system, this will be lowercase sqlplus From Windows or MS-DOS, you may need to use PLUS80 or PLUS80W Beginning with release 8.1, the command under Windows or from a DOS prompt will always be SQLPLUS.
-S[ILENT]
Tells SQL*Plus to run in silent mode No startup messages, such as the copyright message, will be displayed No
command prompt will be displayed, and no commands will be echoed to the screen This is useful if you are invoking SQL*Plus from within some other program, and you want to do it transparently Normally you would use this option in conjunction with invoking a script file
username
Is your database username
password
Is your database password
connect
Is the connect string, or host string, telling SQL*Plus the database to which you want to connect
/ Use a forward slash instead of your username, password, and connect string when you want to connect to a local database using operating system authentication
/NOLOG
Tells SQL*Plus not to connect you to any database at all You will get a SQL> prompt, but you must issue a
CONNECT command before you can do much else
scriptfile
Is the name of a SQL*Plus script file you want to run SQL*Plus will start up, execute the file, and then exit
arg1 agr2 arg3
Are optional command-line arguments to pass to your script You can have as many as your script requires Arguments are separated from each other by at least one space
- Causes SQL*Plus to display a short summary of the SQLPLUS syntax
-? Causes SQL*Plus to display version and copyright information
Trang 10
< previous page page_26 next page >
Page 26
The following examples show the use of the - and -? options:
C:\WINDOWS>PLUS80 -
Usage: SQLPLUS [<option>] [<user>[/<pasword>] [@<host>]]
[@<startfile> [<parm1>] [<parm2>] ]
where <option> ::= { -s ¦ -? }
_s for silent mode and -? to obtain version number
C:\WINDOWS>PLUS80 -?
SQL*PLUS: Release 8.0.4.0.0 - Production on Sun Nov 1 12:14:29 1998
(c) Copyright 1997 Oracle Corporation All rights reserved
C:\WINDOWS>
Information on running script files from SQL*Plus can be found later in this chapter in the section titled Executing a
Script, and also in Chapter 4, Writing SQL*Plus Scripts.
Some Basic SQL*Plus Commands
Now that you know how to start SQL*Plus, it's time to learn a few basic commands This section will show you how to exit SQL*Plus, how to switch your database connection to another database, how to get help, and how to view a
database table definition
All SQL*Plus commands are case-insensitive; you may enter them using either lowercase or uppercase In this book, commands are shown in uppercase to make them stand out better in the examples Be aware, however, that when a filename is used as an argument to a command, it may or may not
be case-sensitive, depending on the specific operating system you are using For example, under Windows NT, filenames are not case-sensitive, but under Unix, they are
Exit.
A good place to start, since you've just seen how to start SQL*Plus, might be with the EXIT command The EXIT command terminates your SQL*Plus session, and either closes the SQL*Plus window (GUI version) or returns you to the operating system prompt Used in its simplest form, the EXIT command looks like this:
SQL> EXIT
Disconnected from Personal Oracle8 Release 8.0.3.0.0 - Production
With the Partitioning option
PL/SQL Release 8.0.3.0.0 - Production