Index Search Add FAQ Ask Question
Index Search Add FAQ Ask Question
$Date: 18-Dec-2002 $
$Revision: 1.66 $
$Author: Frank Naudé $
Topics
SQL*Plus is a command line SQL and PL/SQL language interface and reporting tool that
ships with the Oracle Database Client and Server. It can be used interactively or driven
from scripts. SQL*Plus is frequently used by DBAs and Developers to interact with the
Oracle database.
If you are familiar with other databases, sqlplus is equivalent to "sql" in Ingres, "isql" in
Sybase and SQLServer, "db2" in IBM DB2, "psql" in PostgresQL, and "mysql" in
MySQL.
SQL*Plus's predecessor was called UFI (User Friendly Interface). UFI was included in
the first releases of Oracle, its interface was extremely primitive and, in today's terms,
anything but user friendly.
Back to top of file
Start using SQL*Plus by executing the "sqlplus" command-line utility. Valid options are:
userid/password@db -- Connection details
/nolog -- Do not login to Oracle. You will need to do it yourself.
-s or -silent -- start sqlplus in silent mode. Not recommended for beginners!
@myscript -- Start executing script called "myscript.sql"
Look at this example session:
sqlplus /nolog
SQL> connect scott/tiger
SQL> select * from tab;
SQL> disconnect
SQL> exit
Please note that one must prepare the environment before starting sqlplus. Linux/ Unix
example:
$ . oraenv
ORACLE_SID = [orcl] ? orcl
$ sqlplus scott/tiger
Windows Example:
Click on "Start" -> "Run" and enter "cmd"
C:> set ORACLE_SID=orcl
C:> sqlplus scott/tiger
Back to top of file
One can enter three kinds of commands from the SQL*Plus command prompt:
SHOW USER
2. SQL commands - for more information see the Oracle SQL FAQ. Eg:
3. PL/SQL blocks - for more information see the Oracle PLSQL FAQ. Eg:
4. BEGIN
5. DBMS_OUTPUT.PUT_LINE('Hello World!');
6. END;
7. /
Back to top of file
What is AFIEDT.BUF?
AFIEDT.BUF is the SQL*Plus default edit save file. When you issue the command "ed"
or "edit" without arguments, the last SQL or PL/SQL command will be saved to a file
called AFIEDT.BUF and opened in the default editor.
In the prehistoric days when SQL*Plus was called UFI, the file name was "ufiedt.buf",
short for UFI editing buffer. When new features were added to UFI, it was the initially
named Advanced UFI and the filename was changed to "aufiedt.buf" and then to
"afiedt.buf". They presumably needed to keep the name short for compatibility with some
of the odd operating systems that Oracle supported in those days. The name "Advanced
UFI" was never used officially, as the name was changed to SQL*Plus before this version
was released.
You can overwrite the default edit save file's name like this:
SET EDITFILE "afiedt.buf"
Back to top of file
One can edit SQL scripts and the command buffer (the last command entered) with the
EDIT (or ED) command. However, sometimes one needs select a editor before using this
command. Examples:
DEFINE _EDITOR=vi -- Use the Unix vi-editor
DEFINE _EDITOR=notepad -- Use the Notepad on Windows
TIP: Add this command in your login.sql or glogin.sql scripts so it executes every time
you start sqlplus.
Back to top of file
select info
from system.help
where upper(topic)=upper('&1')
/
Whenever you need help, you can now run the help.sql script:
@help SELECT
Back to top of file
There is no difference. Both "?" and HELP will read the SYSTEM.HELP table (if
available) and shows help text on the screen.
To use the help facility, type HELP followed by the command you need to learn more
about. For example, to get help on the SELECT statement, type:
HELP SELECT
Back to top of file
The @ (at symbol) is equivalent to the START command and is used to run SQL*Plus
command scripts.
A single @ symbol runs a script in the current directory (or one specified with a full or
relative path, or one that is found in you SQLPATH or ORACLE_PATH).
@@ will start a sqlplus script that is in the same directory as the script that called it
(relative to the directory of the current script). This is normally used for nested command
files.
Back to top of file
"&" is used to create a temporary substitution variable and will prompt you for a value
every time it is referenced.
"&&" is used to create a permanent substitution variable as with the DEFINE command
and the OLD_VALUE or NEW_VALUE clauses of a COLUMN statement. Once you
have entered a value it will use that value every time the variable is referenced.
Eg: SQL> SELECT * FROM TAB WHERE TNAME LIKE '%&TABLE_NAME.%';
Back to top of file
Both "!" and "HOST" will execute operating system commands as child processes of
SQL*Plus. The difference is that "HOST" will perform variable substitution (& and &&
symbols), whereas "!" will not. (Note: use "$" under MVS, VMS, and Windows
environments, not "!")
Back to top of file
When SQL*Plus starts up, it will look for a global login script called glogin.sql in the
$ORACLE_HOME/sqlplus/admin directory. If found, this script will be executed.
Thereafter, sqlplus will try to find a local login script called login.sql. It will look for the
login.sql script in the directory where you start sqlplus from, alternatively it will search
the directories listed in the SQLPATH environment variable for such a script. When
found, sqlplus will execute it.
Back to top of file
Can one set the SQL*Plus command prompt to something more useful?
One can change the default 'SQL> ' prompt by changing the SQLPROMPT setting. For
example:
SET SQLPROMPT 'Enter SQLPlus Command> '
The following example script changes the prompt to include the connected username and
database:
undefine usr db
col usr new_value usr
col db new_value db
If you run a script that contains "&" symbols SQL*Plus thinks that you want to prompt
the user for a value. To turn this off:
SET ESCAPE ON
SET ESCAPE "\"
SELECT 'You \& me' FROM DUAL;
or
SET DEFINE ?
SELECT 'You & me' FROM DUAL;
Note: You can disable substitution variable prompting altogether by issuing the SET
DEFINE OFF command.
Back to top of file
Use the "WHENEVER SQLERROR ..." command to trap SQL and PL/SQL errors, and
the "WHENEVER OSERROR ..." to trap operating system errors. Eg:
SQL> WHENEVER OSERROR EXIT 9
SQL> WHENEVER SQLERROR EXIT SQL.SQLCODE
Back to top of file
1. Run the PLUSTRCE.SQL script from the SYS database user. This script is
located in the $ORACLE_HOME/sqlplus/admin directory.
2. Create a PLAN_TABLE table by running the UTLXPLAN.SQL script. This script
is in $ORACLE_HOME/rdbms/admin.
3. Use the "SET AUTOTRACE ON" command to trace SQL execution. This will
print the result of your query, an explain plan and high level trace information.
Look at this example:
4. SQL> set autotrace on
5. SQL> select * from dual;
6.
7. D
8. -
9. X
10.
11. Execution Plan
12. ----------------------------------------------------------
13. 0 SELECT STATEMENT Optimizer=CHOOSE
14. 1 0 TABLE ACCESS (FULL) OF 'DUAL'
15.
16. Statistics
17. ----------------------------------------------------------
18. 0 recursive calls
19. 2 db block gets
20. 1 consistent gets
21. 0 physical reads
22. 0 redo size
23. 181 bytes sent via SQL*Net to client
24. 256 bytes received via SQL*Net from client
25. 3 SQL*Net roundtrips to/from client
26. 0 sorts (memory)
27. 0 sorts (disk)
28. 1 rows processed
Back to top of file
Use the "STORE SET" command to write current settings (SHOW ALL) to a file. This
file can later be executed to restore all settings. Look at the following example (Oracle8
and above):
SQL> STORE SET filename REPLACE
SQL> (do whatever you like)
SQL> @filename
Back to top of file
SQL*Plus tries to format data from the database into a humanly readable format. This
formatting can be disabled by issuing the following SET commands:
SET ECHO OFF
SET NEWPAGE 0
SET SPACE 0
SET PAGESIZE 0
SET FEEDBACK OFF
SET HEADING OFF
SET TRIMSPOOL ON
These settings can also be abbreviated and entered on one line, eg.:
SET ECHO OFF NEWP 0 SPA 0 PAGES 0 FEED OFF HEAD OFF TRIMS
ON
Back to top of file
One can pass operating system variables to sqlplus using this syntax:
sqlplus username/password @cmdfile.sql var1 var2 var3
Parameter var1 will be mapped to SQL*Plus variable &1, var2 to &2, etc. Look at this
example:
sqlplus scott/tiger @x.sql '"test parameter"' dual
Where x.sql consists of:
select '&1' from &2;
exit 5;
Back to top of file
SQL*Plus by default only shows the first 80 bytes of any LONG, CLOB and NCLOB
datatypes. The data is there, but since sqlplus is a command-line tool it tries not to print
out too much data. You can override this to tell sqlplus exactly how many bytes you want
to see:
How does one copy data from one database to another in SQL*Plus?
The SQL*Plus COPY command is one of the fastest ways of copying data between
databases and schemas. This is also one of the few methods that will handle LONG
columns correctly. Look at this example:
COPY FROM SCOTT/TIGER@LOCAL_DB TO
SCOTT/TIGER@REMOTE_DB -
CREATE IMAGE_TABLE USING -
SELECT IMAGE_NO, IMAGE -
FROM IMAGES;
Back to top of file