SQL*Plus的使用环境是可以通过login.sql 或 glogin.sql脚本来设置的,可能很多初学者或不习惯使用SQL*Plus的老鸟都不知道。因为在如今UI工具(Toad、PL/SQL Developer..)大行其道的年代,SQL*Plus这种命令工具渐渐被冷落了,可能只是偶尔被用用,不过这并不妨碍它被很多DBA或SQL*Plus爱好者广泛使用。
其中glogin.sql文件是全局设置文件,位于$ORACLE_HOME/sqlplus/admin下,而login.sql文件属于个性化设置文件,则可以位于任何位置。既可以通过SQLPATH环境变量设置或不设置。
SQL*Plus启动的时候首先会先运行glogin.sql脚本,然后查找当前目录下是否存在login.sql文件,如果找到则运行该脚本,如果当前目录不存在login.sql.则查找是否设置了SQLPATH环境变量,找到了就会去执行该环境变量路径下的login.sql脚本,否则则会停止继续查找。
我们首先来看看glogin.sql文件的庐山正面目吧!其实都是一些格式化列标题和列数据的现实格式的设置,
--
-- Copyright (c) 1988, 2004, Oracle Corporation. All Rights Reserved. -- -- NAME -- glogin.sql -- -- DESCRIPTION -- SQL*Plus global login "site profile" file -- -- Add any SQL*Plus commands here that are to be executed when a -- user starts SQL*Plus, or uses the SQL*Plus CONNECT command -- -- USAGE -- This script is automatically run ---- Used by Trusted Oracle
COLUMN ROWLABEL FORMAT A15-- Used for the SHOW ERRORS command
COLUMN LINE/COL FORMAT A8 COLUMN ERROR FORMAT A65 WORD_WRAPPED-- Used for the SHOW SGA command
COLUMN name_col_plus_show_sga FORMAT a24 COLUMN units_col_plus_show_sga FORMAT a15 -- Defaults for SHOW PARAMETERS COLUMN name_col_plus_show_param FORMAT a36 HEADING NAME COLUMN value_col_plus_show_param FORMAT a30 HEADING VALUE-- Defaults for SHOW RECYCLEBIN
COLUMN origname_plus_show_recyc FORMAT a16 HEADING 'ORIGINAL NAME' COLUMN objectname_plus_show_recyc FORMAT a30 HEADING 'RECYCLEBIN NAME' COLUMN objtype_plus_show_recyc FORMAT a12 HEADING 'OBJECT TYPE' COLUMN droptime_plus_show_recyc FORMAT a19 HEADING 'DROP TIME'-- Defaults for SET AUTOTRACE EXPLAIN report
-- These column definitions are only used when SQL*Plus -- is connected to Oracle 9.2 or earlier. COLUMN id_plus_exp FORMAT 990 HEADING i COLUMN parent_id_plus_exp FORMAT 990 HEADING p COLUMN plan_plus_exp FORMAT a60 COLUMN object_node_plus_exp FORMAT a8 COLUMN other_tag_plus_exp FORMAT a29 COLUMN other_plus_exp FORMAT a44-- Default for XQUERY
COLUMN result_plus_xquery HEADING 'Result Sequence'如果你想定制一些通用的环境变量设置,那么就可以在glogin.sql里面设置,需要注意的是它的影响是全局的。如果你想定制个性化的环境变量,最好使用login.sql来设置。
如果没有loging.sql,通常我们用SQL*Plus登录数据库如下所示:
[oracle@GSP monitoring]$ sqlplus / as sysdba
SQL*Plus: Release 10.2.0.4.0 - Production on Fri Aug 2 14:26:36 2013
Copyright (c) 1982, 2007, Oracle. All Rights Reserved.
Connected to:
Oracle Database 10g Release 10.2.0.4.0 - ProductionSQL>
在当前目录新建一个loging.sql文件,设置了一些变量:
set sqlprompt "_connect_identifier> "
set pagesize 500 set linesize 138 set serveroutput on_connect_identifier: 登录的实例
pagesize: 设置每页打印的行数,该值包括NEWPAGE 设置的空行数 linesize: 设置每行打印字符数,默认每行打印80个字符。可以设置LINESIZE,以避免折叠显示 .....注意下面的明显变化(红色部分)
[oracle@GSP monitoring]$ sqlplus / as sysdbaSQL*Plus: Release 10.2.0.4.0 - Production on Fri Aug 2 14:33:44 2013
Copyright (c) 1982, 2007, Oracle. All Rights Reserved.
Connected to:
Oracle Database 10g Release 10.2.0.4.0 - Productionapps>
如果你想显示SQL*Plus登录的username、instance、datetie,那么你可以修改login.sql文件。
set sqlprompt "_USER@ _CONNECT_IDENTIFIER >" set pagesize 500 set linesize 138 set serveroutput on此时登录SQL*Plus如下所示:
[oracle@GSP monitoring]$ sqlplus / as sysdbaSQL*Plus: Release 10.2.0.4.0 - Production on Fri Aug 2 14:41:07 2013
Copyright (c) 1982, 2007, Oracle. All Rights Reserved.
Connected to:
Oracle Database 10g Release 10.2.0.4.0 - ProductionSYS@ apps >
当然你在其它目录(非当前目录)登录SQL*Plus,则不会有上面红色的环境设置。此时如果你需要在任何目录登录SQL*Plus都能应用该设置,则需设置SQLPATH环境变量。
[oracle@GSP ~]$ vi ~/.bash_profile export SQLPATH=/home/oracle/monitoring/db_com_sql:$ORACLE_HOME/sqlplus/admin下面是一个比较通用的login.sql脚本:
define_editor=vi
set serveroutput on size 100000 set trimspool on set long 5000 set linesize 120 set pagesize 9999 column plan_plus_exp format a80 column global_name new_value gname set termout off define gname=idle column global_name new_value gname select lower(user) || '@' || substr(global_name,1,decode(dot,0,length(global_name),dot-1)) global_name from (select global_name,instr(global_name,'.') dot from global_name); set sqlprompt '&gname> ' set termout on参考资料: