Вы находитесь на странице: 1из 4

Useful SQL*Plus Commands Useful SQL*Plus Commands

---------------------------------------------------------------------- ----------------------------------------------------------------------
Something's wrong with my CLOB/ NCLOB/ LONG column. Where is my How does one use the SQL*Plus utility?
data?
Start using SQL*Plus by executing the "sqlplus" command-line
SQL*Plus by default only shows the first 80 bytes of any LONG, utility from the $ORACLE_HOME/bin directory. Some of the command
CLOB and NCLOB datatypes. The data is there, but since sqlplus is line options:
a command-line tool it tries not to print out too much data. You
userid/password@db -- Connection details
can override this to tell sqlplus exactly how many bytes you want
/nolog -- Do not login to Oracle. You will need to do
to see:
-- it yourself.
SET LONG 32000 -s or -silent -- start sqlplus in silent mode. Not
SELECT text FROM user_views WHERE rownum = 1; -- recommended for beginners!
@myscript -- Start executing script called myscript.sql"
How does one copy data from one database to another in SQL*Plus?
sqlplus /nolog
SQL> connect scott/tiger
The SQL*Plus COPY command is one of the fastest ways of copying SQL> select * from tab;
data between databases and schemas. This is also one of the few SQL> disconnect
methods that will handle LONG columns correctly. Look at this SQL> exit
example:
COPY FROM SCOTT/TIGER@LOCAL_DB TO SCOTT/TIGER@REMOTE_DB - What commands can be executed from SQL*Plus?
CREATE IMAGE_TABLE USING -
SELECT IMAGE_NO, IMAGE -
One can enter three kinds of commands from the SQL*Plus command
FROM IMAGES;
prompt:
SQL*Plus commands
Can one generate HTML reports from SQL*Plus?
SQL*Plus commands are used to set options for SQL*Plus, format
reports, edit files, edit the command buffer, and so on. SQL*Plus
One can generate static HTML pages from SQL*Plus (8.1.6 and
commands do not interact with the database. These commands do not
above) by setting the MARKUP option to HTML ON. This can be done
have to be terminated with a semicolon (;) (as is the case with
by specifying -MARKUP "HTML ON" from command line, or with the
SQL commands). Examples:
"SET MARKUP HTML ON" command. Look at this example SQL Script:
SQL> CLEAR SCREEN
set markup HTML on
SQL> SHOW USER
spool index.html
SQL> SET PAGESIZE 100
select * from tab;
SQL> START myscrip.sql
spool off
set markup HTML off SQL commands
The spooled index.html will be an HTML file that can be opened in
any browser
SQL> SELECT * FROM user_tables;
PL/SQL blocks
BEGIN
dbms_output.put_line('Hello World!');
END;
/

What are the basic SQL*Plus commands?


Here is a list of some of the most frequently used SQL*Plus
commands:
• ACCEPT - Get input from the user

Focus Training Services 8/8 Focus Training Services 1/8


Useful SQL*Plus Commands Useful SQL*Plus Commands
---------------------------------------------------------------------- ----------------------------------------------------------------------
• DEFINE - Declare a variable (short: DEF) Use the "STORE SET" command to write current settings (SHOW ALL)
to a file. This file can later be executed to restore all
• DESCRIBE - Lists the attributes of tables and other settings. Look at the following example (Oracle8 and above):
objects (short: DESC)
SQL> STORE SET filename REPLACE
• EDIT - Places you in an editor so you can edit a SQL SQL> (do whatever you like)
command (short: ED) SQL> @filename
• EXIT or QUIT - Disconnect from the database and terminate
SQL*Plus How does one disable SQL*Plus formatting?

• GET - Retrieves a SQL file and places it into the SQL


buffer SQL*Plus tries to format data from the database into a human
friendly format. This formatting can be disabled by issuing the
• HOST - Issue an operating system command (short: !) following SET commands:
• LIST - Displays the last command executed/ command in the SET ECHO OFF
SQL buffer (short: L) SET NEWPAGE 0
SET SPACE 0
• PROMPT - Display a text string on the screen. Eg prompt SET PAGESIZE 0
Hello World!!! SET FEEDBACK OFF
SET HEADING OFF
• RUN - List and Run the command stored in the SQL buffer SET TRIMSPOOL ON
(short: /) SET TAB OFF
• SAVE - Saves command in the SQL buffer to a file. Eg These settings can also be abbreviated and entered on one line,
"save x" will create a script file called x.sql eg.:
• SET - Modify the SQL*Plus environment eg. SET PAGESIZE 23 SET ECHO OFF NEWP 0 SPA 0 PAGES 0 FEED OFF HEAD OFF TRIMS ON TAB
OFF
• SHOW - Show environment settings (short: SHO). Eg SHOW
ALL, SHO PAGESIZE etc. You may also want to run the "show all" command to display all
the other sqlplus settings that can be changed.
• SPOOL - Send output to a file. Eg "spool x" will save
STDOUT to a file called x.lst
Can one pass operating system parameters to SQL*Plus?
• START - Run a SQL script file (short: @)
One can pass operating system variables to sqlplus using this
What is AFIEDT.BUF? syntax:
sqlplus username/password @cmdfile.sql var1 var2 var3
AFIEDT.BUF is the SQL*Plus default edit save file. When you issue
the command "ed" or "edit" without arguments, the last SQL or Parameter var1 will be mapped to SQL*Plus variable &1, var2 to
PL/SQL command will be saved to a file called AFIEDT.BUF and &2, etc. Look at this example:
opened in the default editor. sqlplus scott/tiger @x.sql '"test parameter"' dual
In the prehistoric days when SQL*Plus was called UFI (User Where x.sql consists of:
Friendly Interface) this file was named "ufiedt.buf", short for
UFI editing buffer. When new features were added to UFI, it was select '&1' from &2;
the initially named Advanced UFI and the filename was changed to exit 5;
"aufiedt.buf" and then to "afiedt.buf". They presumably needed to Example passing the Windows User's Temp Path Location to sqlplus:
keep the name short for compatibility with some of the odd
operating systems that Oracle supported in those days. The name sqlplus username/password @cmdfile.sql %TEMP%
"Advanced UFI" was never used officially, as the name was changed within @cmdfile.sql -
to SQL*Plus before this version was released.
SPOOL &1\myscript.log
You can overwrite the default edit save file's name like this: -- Your script commands
SET EDITFILE "afiedt.buf" SPOOL OFF

Focus Training Services 2/8 Focus Training Services 7/8


Useful SQL*Plus Commands Useful SQL*Plus Commands
---------------------------------------------------------------------- ----------------------------------------------------------------------
---------- -------------- ------------- I'm unable to edit files. What is wrong?
40 OPERATIONS BOSTON
One can edit SQL scripts and the command buffer (the last command
Execution Plan
entered) with the EDIT (or ED) command. However, sometimes one
----------------------------------------------------------
needs to select an editor before using this command. Examples:
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=1 Card=1
Bytes=18) Use the Unix/Linux vi-editor:
1 0 TABLE ACCESS (BY INDEX ROWID) OF 'DEPT' (Cost=1
Card=1 Bytes=18) DEFINE _EDITOR=vi
2 1 INDEX (UNIQUE SCAN) OF 'PK_DEPT' (UNIQUE) Use the Notepad on Windows:

Statistics DEFINE _EDITOR=notepad


---------------------------------------------------------- TIP: Add this command in your login.sql or glogin.sql scripts so
0 recursive calls it executes every time you start sqlplus.
0 db block gets
2 consistent gets
0 physical reads What is the difference between @ and @@?
0 redo size
499 bytes sent via SQL*Net to client The @ (at symbol) is equivalent to the START command and is used
503 bytes received via SQL*Net from client to run SQL*Plus command scripts.
2 SQL*Net roundtrips to/from client
0 sorts (memory) SQL> @myscript.sql
0 sorts (disk)
A single @ symbol runs a script in the current directory (or one
1 rows processed
specified with a full or relative path, or one that is found in
Follow these steps to enable this feature: your SQLPATH or ORACLE_PATH).
Run the PLUSTRCE.SQL script from the SYS database user. @@ will start a sqlplus script that is in the same directory as
This script is located in the $ORACLE_HOME/sqlplus/admin the script that called it (relative to the directory of the
directory. current script). This is normally used for nested command files.
This technique is commonly used by scripts that call subscripts
Create a PLAN_TABLE table by running the UTLXPLAN.SQL script. in the ?/rdbms/admin directory.
This script is in $ORACLE_HOME/rdbms/admin.
Use the "SET AUTOTRACE ON" command to trace SQL execution. This What is the difference between & and &&?
will print an explain plan and high level trace information after
your query results.
"&" is used to create a temporary substitution variable that will
prompt you for a value every time it is referenced. Example:
Can one prevent users from executing devious commands?
SQL> SELECT sal FROM emp WHERE ename LIKE '&NAME';
Enter value for name: SCOTT
Yes, command authorization is verified against the old 1: SELECT sal FROM emp WHERE ename LIKE '&NAME'
SYSTEM.PRODUCT_USER_PROFILE table. This table is created by the new 1: SELECT sal FROM emp WHERE ename LIKE 'SCOTT'
PUPBLD.SQL script. Note that this table is not used when someone SAL
signs on as user SYSTEM. ----------
Eg. to disable all users whose names starts with OPS$ from 3000
executing the CONNECT command:
SQL> /
SQL> INSERT INTO SYSTEM.PRODUCT_USER_PROFILE Enter value for name: SCOTT
VALUES ('SQL*Plus', 'OPS$%', 'CONNECT', NULL, NULL, old 1: SELECT sal FROM emp WHERE ename LIKE '&NAME'
'DISABLED', NULL, NULL); new 1: SELECT sal FROM emp WHERE ename LIKE 'SCOTT'
SAL
How does one restore session state in SQL*Plus? ----------
3000

Focus Training Services 6/8 Focus Training Services 3/8


Useful SQL*Plus Commands Useful SQL*Plus Commands
---------------------------------------------------------------------- ----------------------------------------------------------------------
"&&" is used to create a permanent substitution variable. Once NOTE: From Oracle 10g SQL*Plus will attempt to execute glogin.sql
you have entered a value (defined the variable) its value will and login.sql after each successful connection. This is handy if
used every time the variable is referenced. Example: you want to change the sqlprompt to include the current user.
Here is an example (g)login.sql file:
SQL> SELECT sal FROM emp WHERE ename LIKE '&&NAME';
Enter value for name: SCOTT prompt Loading login.sql file...
old 1: SELECT sal FROM emp WHERE ename LIKE '&&NAME' set sqlprompt "&&_USER@&&_CONNECT_IDENTIFIER SQL>"
new 1: SELECT sal FROM emp WHERE ename LIKE 'SCOTT' define _editor=vi

SAL A bit of history: when SQL*Plus was still called UFI, this file
was called login.ufi (located in $ORACLE_HOME/demo).
----------
3000
How does one disable interactive prompting in SQL*Plus?
SQL> /
old 1: SELECT sal FROM emp WHERE ename LIKE '&&NAME'
new 1: SELECT sal FROM emp WHERE ename LIKE 'SCOTT' If you run a script that contains "&" symbols, SQL*Plus thinks
SAL that you want to prompt the user for a value. Some clients allow
---------- one to escape the ampersand character with a backslash, however,
3000 that doesn't work from SQL*Plus. Here is a couple of solutions:

The "&&" will actually define the variable similarly to what the SET ESCAPE ON
DEFINE command or OLD_VALUE/ NEW_VALUE clauses of a COLUMN SET ESCAPE ""
statement would have done. SELECT 'You & me' FROM DUAL;

SQL> define or
DEFINE NAME = "SCOTT" (CHAR) SET DEFINE ?
SELECT 'You & me' FROM DUAL;
What is the difference between ! and HOST? Atanas Kebedjiev provided this solution:
SELECT 'You '||Chr(38)||' Me' FROM DUAL;
Both "!" and "HOST" will execute operating system commands as
child processes of SQL*Plus. The difference is that "HOST" will Note: You can disable substitution variable prompting altogether
perform variable substitution (& and && symbols), whereas "!" by issuing the SET DEFINE OFF or SET SCAN OFF command.
will not. Examples:
SQL> ! whoami How does one trap errors in SQL*Plus?
oracle
SQL> DEFINE cmd="whoami" Use the "WHENEVER SQLERROR ..." command to trap SQL and PL/SQL
SQL> HOST &&cmd errors, and the "WHENEVER OSERROR ..." to trap operating system
oracle errors. Eg:
Note: use "$" under OS/390, VMS, and Windows environments, not SQL> WHENEVER OSERROR EXIT 9
"!". SQL> WHENEVER SQLERROR EXIT SQL.SQLCODE

Can one run commands when SQL*Plus starts up? How does one trace (and explain) SQL statements from SQL*Plus?

When SQL*Plus starts up, it looks for a global login script Method 1: Autotrace Facility
called glogin.sql in the $ORACLE_HOME/sqlplus/admin directory. If
found, this script will be executed. When the AUTOTRACE setting is enabled, SQL*Plus will print an
EXPLAIN PLAN and execution statistics after each SQL statement.
Thereafter, sqlplus will try to find a local login script called Look at this example:
login.sql in the directory where you start sqlplus from,
alternatively the directories listed in the SQLPATH environment SQL> set autotrace on
variable. When found, sqlplus will execute it. SQL> select * from dept where deptno = 40;
DEPTNO DNAME LOC

Focus Training Services 4/8 Focus Training Services 5/8