`

SQL*Plus FAQ

阅读更多
 
   

SQL*Plus FAQ

From Oracle FAQ

<!---->

SQL*Plus FAQ:

Contents

[hide]
<script type="text/javascript"></script>

What is SQL*Plus and where does it come from?

SQL*Plus is a command line SQL and PL/SQL language interface and reporting tool that ships with the Oracle Database Client and Server software. 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*Plus's predecessor was called UFI (User Friendly Interface). UFI was included in the first Oracle releases up to Oracle 4. The UFI interface was extremely primitive and, in today's terms, anything but user friendly. If a statement was entered incorrectly, UFI issued an error and rolled back the entire transaction (ugggh).

How does one use the SQL*Plus utility?

Start using SQL*Plus by executing the "sqlplus" command-line utility. Some of the command line options:

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

or...

C:> sqlplus scott/tiger@orcl

What commands can be executed from SQL*Plus?

One can enter three kinds of commands from the SQL*Plus command prompt:

SQL*Plus commands

SQL*Plus commands are used to set options for SQL*Plus, format reports, edit files, edit the command buffer, and so on. SQL*Plus commands do not interact with the database. These commands do not have to be terminated with a semicolon (;) (as is the case with SQL commands). Examples:

SQL> CLEAR SCREEN
SQL> SHOW USER
SQL> SET PAGESIZE 100
SQL> START myscrip.sql

SQL commands

For more information see the Oracle [SQL FAQ]. Eg:

SQL> SELECT * FROM user_tables;

PL/SQL blocks

For more information see the Oracle [PLSQL FAQ]. Eg:

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
  • DEFINE - Declare a variable (short: DEF)
  • DESCRIBE - Lists the attributes of tables and other objects (short: DESC)
  • EDIT - Places you in an editor so you can edit a SQL command (short: ED)
  • EXIT or QUIT - Disconnect from the database and terminate SQL*Plus
  • GET - Retrieves a SQL file and places it into the SQL buffer
  • HOST - Issue an operating system command (short: !)
  • LIST - Displays the last command executed/ command in the SQL buffer (short: L)
  • PROMPT - Display a text string on the screen. Eg prompt Hello World!!!
  • RUN - List and Run the command stored in the SQL buffer (short: /)
  • SAVE - Saves command in the SQL buffer to a file. Eg "save x" will create a script file called x.sql
  • SET - Modify the SQL*Plus environment eg. SET PAGESIZE 23
  • SHOW - Show environment settings (short: SHO). Eg SHOW ALL, SHO PAGESIZE etc.
  • SPOOL - Send output to a file. Eg "spool x" will save STDOUT to a file called x.lst
  • START - Run a SQL script file (short: @)

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 (User Friendly Interface) this file was named "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"

I'm unable to edit files. What is wrong?

One can edit SQL scripts and the command buffer (the last command entered) with the EDIT (or ED) command. However, sometimes one needs to select a editor before using this command. Examples:

Use the Unix/Linux vi-editor:

DEFINE _EDITOR=vi

Use the Notepad on Windows:

DEFINE _EDITOR=notepad

TIP: Add this command in your login.sql or glogin.sql scripts so it executes every time you start sqlplus.

How does one enable the SQL*Plus HELP facility?

To enable HELP for SQl*Plus, run the supplied "helpins" script in $ORACLE_HOME/bin. The "helpins" command will prompt you for the SYSTEM password and load the help data into the SYSTEM schema.

Alternatively you can load the help facility manually like this:

cd $ORACLE_HOME/sqlplus/admin/help
sqlplus system/manager @helpdrop.sql        # Drop the HELP table
sqlplus system/manager @helpbld.sql         # Create the HELP table
sqlplus system/manager @helpus.sql          # Load data into the HELP table

If the HELP command is not supported on your operating system, you can access the help table with a simple script like this (let's call it help.sql):

select info
from   system.help
where  upper(topic)=upper('&1');

Whenever you need help, you can now run the help.sql script:

@help SELECT

What is the difference between ? and HELP?

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

One can also extend the help system by inserting information into the HELP table. Look at this example:

SQL> insert into help values ('MYTOPIC', 1, 'Detail line 1');
1 row created.

SQL> insert into help values ('MYTOPIC', 2, 'Detail line 2');
1 row created.

SQL> help MYTOPIC
Detail line 1
Detail line 2

SQL> ? MYTOPIC
Detail line 1
Detail line 2

What is the difference between @ and @@?

The @ (at symbol) is equivalent to the START command and is used to run SQL*Plus command scripts.

SQL> @myscript.sql

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. This technique is commonly used by scripts that call subscripts in the ?/rdbms/admin directory.

What is the difference between & and &&?

"&" is used to create a temporary substitution variable that will prompt you for a value every time it is referenced. Example:

SQL> SELECT sal FROM emp WHERE ename LIKE '&NAME';
Enter value for name: SCOTT
old   1: SELECT sal FROM emp WHERE ename LIKE '&NAME'
new   1: SELECT sal FROM emp WHERE ename LIKE 'SCOTT'
       SAL
----------
      3000

SQL> /
Enter value for name: SCOTT
old   1: SELECT sal FROM emp WHERE ename LIKE '&NAME'
new   1: SELECT sal FROM emp WHERE ename LIKE 'SCOTT'
       SAL
----------
      3000

"&&" is used to create a permanent substitution variable. Once you have entered a value (defined the variable) its value will used every time the variable is referenced. Example:

SQL> SELECT sal FROM emp WHERE ename LIKE '&&NAME';
Enter value for name: SCOTT
old   1: SELECT sal FROM emp WHERE ename LIKE '&&NAME'
new   1: SELECT sal FROM emp WHERE ename LIKE 'SCOTT'

       SAL
----------
      3000

SQL> /
old   1: SELECT sal FROM emp WHERE ename LIKE '&&NAME'
new   1: SELECT sal FROM emp WHERE ename LIKE 'SCOTT'
       SAL
----------
      3000

The "&&" will actually define the variable similarly to what the DEFINE command or OLD_VALUE/ NEW_VALUE clauses of a COLUMN statement would have done.

SQL> define
DEFINE NAME            = "SCOTT" (CHAR)

What is the difference between ! and HOST?

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. Examples:

SQL> ! whoami
oracle
SQL> DEFINE cmd="whoami"
SQL> HOST &&cmd
oracle

Note: use "$" under OS/390, VMS, and Windows environments, not "!".

Can one run commands when SQL*Plus starts up?

When SQL*Plus starts up, it looks 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 in the directory where you start sqlplus from, alternatively the directories listed in the SQLPATH environment variable. When found, sqlplus will execute it.

NOTE: From Oracle 10g SQL*Plus will attempt to execute glogin.sql and login.sql after each successful connection. This is handy if you want to change the sqlprompt to include the current user. Here is an example (g)login.sql file:

prompt Loading login.sql file...
set sqlprompt "&&_USER@&&_CONNECT_IDENTIFIER SQL>"
define _editor=vi

A bit of history: when SQL*Plus was still called UFI, this file was called login.ufi (located in $ORACLE_HOME/demo).

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 scripts can be used to include the connected username and database name into the prompt:

For Oracle 10g and above:

set sqlprompt "_USER'@'_CONNECT_IDENTIFIER _PRIVILEGE> "

Pre 10g:

undefine usr db
col usr new_value usr
col db  new_value db

set termout off
select lower(user) usr,
       substr(global_name, 1, instr(global_name, '.')-1) db
  from   global_name
/
set termout on

set sqlprompt '&&usr.@&&db.> '

NOTE: For the above example it might be better to get the database name from v$database. However, all users do not have access to the v$database view.

How does one disable interactive prompting in SQL*Plus?

If you run a script that contains "&" symbols, SQL*Plus thinks that you want to prompt the user for a value. Some clients allow one to escape the ampersand character with a backslash, however, that doesn't work from SQL*Plus. Here is a couple of solutions:

SET ESCAPE ON
SET ESCAPE ""
SELECT 'You & me' FROM DUAL;

or

SET DEFINE ?
SELECT 'You & me' FROM DUAL;

Atanas Kebedjiev provided this solution:

SELECT 'You '||Chr(38)||' Me' FROM DUAL;

Note: You can disable substitution variable prompting altogether by issuing the SET DEFINE OFF or SET SCAN OFF command.

How does one trap errors in SQL*Plus?

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

How does one trace (and explain) SQL statements from SQL*Plus?

Method 1: Autotrace Facility

When the AUTOTRACE setting is enabled, SQL*Plus will print an EXPLAIN PLAN and execution statistics after each SQL statement. Look at this example:

SQL> set autotrace on
SQL> select * from dept where deptno = 40;
    DEPTNO DNAME          LOC
---------- -------------- -------------
        40 OPERATIONS     BOSTON

Execution Plan
----------------------------------------------------------
   0      SELECT STATEMENT Optimizer=CHOOSE (Cost=1 Card=1 Bytes=18)
   1    0   TABLE ACCESS (BY INDEX ROWID) OF 'DEPT' (Cost=1 Card=1 Bytes=18)
   2    1     INDEX (UNIQUE SCAN) OF 'PK_DEPT' (UNIQUE)

Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
          2  consistent gets
          0  physical reads
          0  redo size
        499  bytes sent via SQL*Net to client
        503  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed

Follow these steps to enable this feature:

  • Run the PLUSTRCE.SQL script from the SYS database user. This script is located in the $ORACLE_HOME/sqlplus/admin directory.
  • Create a PLAN_TABLE table by running the UTLXPLAN.SQL script. This script is in $ORACLE_HOME/rdbms/admin.
  • Use the "SET AUTOTRACE ON" command to trace SQL execution. This will print an explain plan and high level trace information after your query results.

Method 2: DBMS_XPLAN Package

SQL> EXPLAIN PLAN FOR select * from dept where deptno = 40;
Explained.

SQL> set linesize 132

SQL> SELECT * FROM TABLE( dbms_xplan.display);

PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------------
Plan hash value: 2852011669

---------------------------------------------------------------------------------------
| Id  | Operation                   | Name    | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |         |     1 |    20 |     1   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| DEPT    |     1 |    20 |     1   (0)| 00:00:01 |
|*  2 |   INDEX UNIQUE SCAN         | PK_DEPT |     1 |       |     0   (0)| 00:00:01 |
---------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("DEPTNO"=40)

14 rows selected.

How can one prevent SQL*Plus connection warning messages?

When I go to SQl*Plus, I get the following errors:

Error accessing PRODUCT_USER_PROFILE
Warning:  Product user profile information not loaded!
You may need to run PUPBLD.SQL as SYSTEM

This messages will stop appearing when you create the PRODUCT_USER_PROFILE table in the SYSTEM schema. This is performed by the PUPBLD.SQL script.

Go to the $ORACLE_HOME/sqlplus/admin directory, connect as SYSTEM and run @PUPBLD.SQL from the sql prompt.

Can one prevent users from executing devious commands?

Yes, command authorization is verified against the SYSTEM.PRODUCT_USER_PROFILE table. This table is created by the PUPBLD.SQL script. Note that this table is not used when someone signs on as user SYSTEM.

Eg. to disable all users whose names starts with OPS$ from executing the CONNECT command:

SQL> INSERT INTO SYSTEM.PRODUCT_USER_PROFILE 
     VALUES ('SQL*Plus', 'OPS$%', 'CONNECT', NULL, NULL, 'DISABLED', NULL, NULL);

How does one restore session state in SQL*Plus?

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

How does one disable SQL*Plus formatting?

SQL*Plus tries to format data from the database into a human friendly 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

You may also want to run the "show all" command to display all the other sqlplus settings that can be changed.

Can one pass operating system parameters to SQL*Plus?

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;

Info received from Tim Kessler:

Since I occasionally get useful info from your site I felt it is appropriate to add the following:

Example passing the Windows User's Temp Path Location to sqlplus:

sqlplus username/password @cmdfile.sql %TEMP%

within @cmdfile.sql -

SPOOL &1\myscript.log
-- Your script commands
SPOOL OFF

Something's wrong with my CLOB/ NCLOB/ LONG column. Where is my data?

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:

SET LONG 32000
SELECT text FROM user_views WHERE rownum = 1;

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;

Can one generate HTML reports from SQL*Plus?

One can generate static HTML pages from SQL*Plus (8.1.6 and above) by setting the MARKUP option to HTML ON. This can be done by specifying -MARKUP "HTML ON" from command line, or with the "SET MARKUP HTML ON" command. Look at this example SQL Script:

set markup HTML on
spool index.html
select * from tab;
spool off
set markup HTML off

The spooled index.html file should look like this:

TNAMETABTYPECLUSTERID
BONUS TABLE  
DEPT TABLE  
EMP TABLE  

Note: You can deploy this file on your web site or edit it in an HTML editor (like FrontPage or Dreamweaver). Another good idea is to develop a CSS to present the data more elegantly. One can also embed HTML tags in the select statement to create hyperlinks and add more HTML features.

<!---->
<!---->
<!---->
<script type="text/javascript"></script>
分享到:
评论

相关推荐

    ORACLE FAQ超级帮助文档.rar

    1. **Oracle基础知识**:Oracle数据库系统由多个组件构成,包括数据库服务器、客户端工具、SQL*Plus、PL/SQL等。理解这些组件的作用以及它们如何协同工作是掌握Oracle的基础。 2. **SQL语言**:Oracle数据库使用...

    Oracle常用傻瓜问题1000问

    8. **PL/SQL FAQ**: PL/SQL是Oracle的编程语言,结合了SQL和过程式编程,常用于编写存储过程、函数、包等。PL/SQL中的异常处理、游标、变量声明、循环结构等是学习的重点。 了解并掌握以上知识点,可以帮助你解决...

    ORACLE-FAQ.rar_cracle_html oracle_oracle

    7. **性能优化**:Oracle提供了许多工具和特性来优化数据库性能,如SQL*Plus、Explain Plan、AWR(自动工作负载 repository)、ASH(Active Session History)等,用于分析和改进SQL查询性能。 8. **安全与备份恢复...

    ORACLE之FAQ -- 性能调整

    ### ORACLE之FAQ -- 性能调整 #### 一、性能调整基础知识 在Oracle数据库管理过程中,性能调整是一项至关重要的工作。它不仅涉及到SQL语句的优化,还包括系统配置、索引策略、统计信息收集等多个方面。下面将详细...

    FAQ-Oracle.zip_oracle

    - **SQL*Plus**: Oracle自带的命令行工具,通过`sqlplus username/password@connect_string`来连接数据库。其中,username是用户名,password是密码,connect_string是数据库连接标识符,可以是服务名(service name...

    XMLDB ファースト・ステップ.pdf

    本书面向已具备XML基础知识并首次尝试构建Oracle XMLDB应用程序的工程师,旨在通过实际操作SQL*Plus来加深对Oracle XMLDB的理解。 ### 2. XML文档存储 - **XMLType存储**:Oracle XMLDB提供了专门的XML数据类型——...

    ORACLE_FAQ(2).rar_oracle

    5. **故障排查**:当遇到错误提示或异常行为时,可以通过查看数据库日志、使用DBA工具(如SQL*Plus、Oracle Enterprise Manager)进行诊断。了解错误代码的含义和解决建议是解决问题的关键。 6. **高可用性与复制**...

    ORACLE之 FAQ

    14. **数据库分析技术**:Oracle提供了多种分析工具,如SQL*Plus、Toad、SQL Developer等,用于性能分析、优化和数据库维护。 15. **Oracle后台进程**:这些进程负责数据库的运行和管理,如系统监控、数据写入、...

    oracle调优工具.rar

    这通常需要使用到如SQL*Plus、 tkprof 或者 Oracle的自动SQL调优工具。 2. **索引优化**:根据数据访问模式合理创建和管理索引,可以显著提升查询速度。 3. **内存管理**:调整SGA(System Global Area)和PGA...

    Oracle实用文档

    Oracle提供了多种备份策略,如物理备份(如RMAN)和逻辑备份(如SQL*Plus的EXPDP/IMPDP)。物理备份通常包括数据文件、控制文件、重做日志文件的备份,而逻辑备份则侧重于表、索引、用户对象等的导出导入。恢复过程...

    oracle参考、使用大全问答集

    通过工具如SQL*Plus、 tkprof、AWR和ASH报告,可以分析性能瓶颈并制定优化策略。 5. Oracle网络: Oracle网络组件如Net Services(TNS)负责数据库的连接和通信。理解网络配置文件(tnsnames.ora、listener.ora)...

    Oracle Instant Client 11.2.0.1.0 轻量级Oracle客户端

    Oracle Instant Client 11.2.0.1.0是轻量级Oracle客户端,用于连接访问Oracle ...SQL*Plus http://download.oracle.com/docs/cd/E11882_01/server.112/e16604/apd.htm#sthref3169 SDK: OCI(Oracle Call Interface) ...

    Jetty权威指南.pdf

    &lt;New id="dataSource" class="org.eclipse.jetty.plus.jndi.Resource"&gt; &lt;Arg&gt;jdbc/myDS &lt;Arg&gt;org.apache.commons.dbcp.BasicDataSource &lt;Set name="url"&gt;jdbc:mysql://localhost:3306/mydb ...

    snort 安装详解

    yum install gcc gcc-c++ make autoconf automake libtool flex bison libpcap-devel libdnet-devel liblog4cplus-devel libyaml-devel -y ``` 2. **下载 Snort 源码**:可以从官方网站下载最新版本的 Snort 源码...

Global site tag (gtag.js) - Google Analytics