August 9, 2018

Setting SQL prompt in SQL*Plus Oracle

Changing SQL prompt in Oracle SQL*PLUS

The default prompt in SQL*Plus is SQL>, does not provide any information like who the user is and what the user is connected as. Prior to Oracle9i, we used to do elaborate coding to get the information as the SQL prompt, from Oracle 9.2.0 we can use SET SQLPROMPT along with SQL*Plus predefined variables.

Whenever SQL * PLUS starts up, it looks for a file named glogin.sql (Site profile script) under the directory $ORACLE_HOME/sqlplus/admin. If the file is found, it is read and the containing statements executed. This allows to store settings across SQL*PLUS sessions. From Oracle 10g, after reading glogin.sql, SQL*PLUS also looks for a file named login.sql (User profile script), in the directory from where SQL*PLUS was and in the directory that the environment variable SQLPATH points to, and reads it and executes it.


In Oracle9i, whenever a user connects through SQL*PLUS, Oracle will execute only glogin.sql, from 10g Oracle will execute login.sql as well. From Oracle 10g, the login.sql file is not only executed at SQL*Plus startup time, but also at connect time as well. So SQL prompt will be changed after connect command.

Settings from the login.sql take precedence over settings from glogin.sql.

To set SQL prompt permanently, update 
      $ORACLE_HOME/sqlplus/admin/glogin.sql or login.sql

To setup SQL prompt for session or permanently, use below SET command.

SET SQLP[ROMPT] {SQL> | text}


TEXT can be predefined substitution variables which are prefixed with an underscore.


_connect_identifierwill display connection identifier.
_datewill display date.
_editorwill display editor name used by the EDIT command.
_o_versionwill display Oracle version.
_o_releasewill display Oracle release.
_privilegewill display privilege such as SYSDBA, SYSOPER, SYSASM, SYSDG, SYSBACKUP, SYSKM, SYSRAC
_sqlplus_releasewill display SQL*PLUS release.
_userwill display current user name.

The variable _CONNECT_IDENTIFIER was introduced in SQL*Plus 9.2
and _DATE, _PRIVILEGE and _USER were introduced in SQL*Plus 10.1.

In complex Oracle Database environments, where Database Adminstrators (DBAs) login into hundreds of databases, it will be useful to change default SQL prompt to some meaningful prompt, like ORACLE SID or database name.

_USER

The variable _USER contains the current user name given by SHOW USER. If SQL*Plus is not connected, the variable is defined as an empty string.

SQL>
set sqlprompt "_user>"

The SQL * Plus prompt will show
SYSTEM>
SATYA>

_PRIVILEGE

When SQL * Plus is connected as a privileged user the variable _PRIVILEGE contains the connection privilege "AS SYSDBA" or "AS SYSOPER" or "AS SYSASM", SYSDG, SYSBACKUP, SYSKM, SYSRAC. If SQL*Plus is connected as a normal user the variable is defined as an empty string.

SQL>
set sqlprompt "_user _privilege>"

The SQL*Plus prompt will show
SYS AS SYSDBA>
SATYA AS SYSDG>
ASMADM AS SYSASM>


_CONNECT_IDENTIFIER

The variable _CONNECT_IDENTIFIER contains the connection identifier used to start SQL*Plus.
For example, if the SQL*Plus connection string is "hr/my_password@MYSID" then the variable contains MYSID. If you use a complete Oracle Net connection string like "hr/my_password@(DESCRIPTION=(ADDRESS_LIST=...(SERVICE_NAME=MYSID.MYDOMAIN)))" then _CONNECT_IDENTIFIER will be set to MYSID.
If the connect identifier is not explicitly specified then _CONNECT_IDENTIFIER contains the default connect identifier Oracle uses for connection. For example, on UNIX it will contain the value in the environment variable ORACLE_SID or TWO_TASK. If SQL*Plus is not connected then the variable is defined as an empty string.

SQL>
set sqlprompt "&_user@&_connect_identifier >"
or
SQL>
set sqlprompt "_user'@'_connect_identifier >"

The SQL * Plus prompt will show
SYS@PROD >
SYSTEM@DWH >


_DATE
The variable _DATE can be either dynamic, showing the current date or it can be set to a fixed string. The date is formatted using the value of NLS_DATE_FORMAT and will show time information. By default a DEFINE or dereference using &_DATE will give the date at the time of use. _DATE can be UNDEFINED, or set to a fixed string with an explicit DEFINE command. 
Dynamic date behavior is re-enabled by defining _DATE to an empty string.

If we want to display current date:
SQL>
set sqlprompt "_user _privilege 'on' _date>"
SYS AS SYSDBA on 9-AUG-18>

If we want to display the current date & time:
SATYA on 9-AUG-18 at DEVDB> alter session set nls_date_format = 'mm/dd/yyyy hh24:mi:ss';
SATYA on 08/09/2018 13:03:51 at DEVDB>


_EDITOR
The variable _EDITOR contains the external text editor executable name.

set sqlprompt _editor>

The SQL*Plus prompt will show
vi>


_O_VERSION
The variable _O_VERSION contains a text string showing the database version and available options.

set sqlprompt _o_version>

The SQL*Plus prompt will show

Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production With the Partitioning, Data Mining and Real Application Testing options>

_O_RELEASE
The variable _O_RELEASE contains a string representation of the Oracle database version number. If Oracle database version is 11.1.0.7.0 then the variable contains "1101000700". The Oracle version may be different from the SQL*Plus version if you use Oracle Net to connect to a remote database.

set sqlprompt _o_release>

The SQL*Plus prompt will show (for 12.1.0.2.0)
1201000200>


_SQLPLUS_RELEASE

The variable _SQLPLUS_RELEASE contains the SQL*Plus version number in a similar format to _O_RELEASE.

set sqlprompt _sqlplus_release>

The SQL*Plus prompt will show (for 11.2.0.4.0)
1102000400>

SQL> set sqlprompt "_user'@'_connect_identifier:SQL> "
SCOTT@DEVDB:SQL> connect system/manager
SYSTEM@DEVDB:SQL> disconnect
@:SQL> connect OEM/OEM@oemdb
OEM@OEMDB:SQL>

To reset to the default SQL prompt,
SQL> set sqlprompt 'SQL>'

To set Oracle Database name as prompt,

col db_name new_value db_name
select name db_name from v$database;
SET SQLPROMPT "&db_name >"


Related Oracle Articles:  
New SQLcl tool commands     Statspack in Oracle

7 comments:

  1. How can I make this sqlprompt as permanent

    ReplyDelete
  2. Hi,

    The answer is already there in this SQL prompt article.

    To set SQL prompt permanently, update $ORACLE_HOME/sqlplus/admin/glogin.sql or login.sql

    ReplyDelete
  3. I've been setting my SQL Prompt in my login.sql for years.
    I use Set SQLPrompt " _USER' @ '_CONNECT_IDENTIFIER > ";
    and I get... JAMES @ My_DB >

    If I run any SQL commands, everything is fine.
    However, if I run a script, my prompt changes to... _USER' @ '_CONNECT_IDENTIFIER >
    and NOT... JAMES @ My_DB >
    I hit enter, and it's back to... JAMES @ My_DB >
    Why does it change, and how do I get it to remain... JAMES @ My_DB >

    Thank-You!
    James

    ReplyDelete
  4. how to change the sqlprompt name permanently.

    ReplyDelete
  5. Satya! Thank you very much . As a fresher iam learning a lot from your blog

    ReplyDelete
  6. Really....It's a great effort from your side. This will definately help all the freshers as well as experienced.
    Thank You,
    Satya

    ReplyDelete