Alex Rivera | Logout

Favorite SQL*Plus tips and tricks

Asked 2009-09-17T14:20:37.657
63

So many times I just need a quick connection to an Oracle DB, where SQLPLUS handles the job.

I imagine when people start using Oracle, the first thing they are told to do is to install Toad or SQLDeveloper. Even so, sometimes you don't want to wait for those tools to load, if you are performing some simple queries.

I have a script that I run when I start my shell so I get a better experience:

SET pagesize 2000
SET LONG 10000
SET linesize 1000
COLUMN last_name format a20
COLUMN total format 999,999,999
SET feedback ON
alter session set nls_date_format = 'yyyy-mm-dd hh:mi:ssPM';

I trimmed my "COLUMN" settings for this example, but basically, that helps the data fit on the screen.

Setting the date format really simplifies dealing with dates.

When the command window opens in Windows, I set the window layout properties so I can scroll, have a wider window, etc. and save the settings for future windows.

Does anybody else use SQL*Plus daily? Any tips?

Edit
Report

2 Answers

8

I use SQL*Plus exclusively to work with Oracle. Other answers already give some very handy login.sql contents.

This is my login.sql. I copied some suggestions from Tom Kyte and William Robertson in there. Maybe you find some things you want to use as well.

set termout off
set serveroutput on size unlimited
set pagesize 50000
set linesize 135
set long 50000
set trimspool on
set tab off
def _editor = "C:\Progra~1\Notepad++\Notepad++.exe"

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> '

alter session set nls_date_format = 'dd-mm-yyyy hh24:mi:ss'
/
var sid number
var serial# number
var tracefile VARCHAR2(200)

DECLARE
    v_audsid v$session.audsid%TYPE;
BEGIN
    SELECT sid, serial#, audsid
    INTO   :sid, :serial#, v_audsid
    FROM   v$session
    WHERE  audsid = SYS_CONTEXT('USERENV','SESSIONID');

    SELECT par.value ||
           CASE WHEN par.value LIKE '%/%' THEN '/' ELSE '\' END ||
           LOWER(th.instance) ||
           '_ora_' || LTRIM(TO_CHAR(pro.spid,'fm99999')) || '.trc' AS filename
    INTO   :tracefile
    FROM   v$process   pro
         , v$session   se
         , v$parameter par
         , v$thread    th
    WHERE  se.audsid = v_audsid
    AND    pro.addr = se.paddr
    AND    par.NAME = 'user_dump_dest';
END;
/

BEGIN
    IF :sid IS NULL THEN
        SELECT sid
        INTO   :sid
        FROM   v$mystat
        WHERE  rownum = 1;
    END IF;
END;
/

set termout on
set feedback off
exec DBMS_OUTPUT.PUT_LINE('Sessie: ' || :sid || CASE WHEN :serial# IS NULL THEN ' (no access to V$ tables)' ELSE ',' || :serial# END)
exec IF :tracefile IS NOT NULL THEN DBMS_OUTPUT.PUT_LINE('Eventueel trace-bestand: ' || :tracefile); END IF
prompt
set feedback on
answered 2009-09-17T20:00:43.563
3

I find it is handy to use SQL*Plus column variables within directives - for example, I'm often in a session and want to spool to a new file name to avoid overwriting another log that may already exist and do this (first three statements through an @file):

SQL> column spr new_value spoolref
SQL> select user||'_'||abs(dbms_random.random) spr from dual;
SQL> spool &spoolref
         ... do work here ...
SQL> spool off

I'll then find the new log by sorting by time - you could always use some strategy other than the random number if you prefer.

answered 2009-09-17T14:54:35.463

Your Answer