SQL*Plus has its own commands and environment, and it provides access to the Oracle Database. It enables you to enter and execute SQL, PL/SQL, SQL*Plus and operating system commands to perform the following: Format, perform calculations on, store, and print from query results. Examine table and object definitions.
SQL*Plus is an Oracle-developed tool that allows you to interactively enter and execute SQL commands and PL/SQL blocks. Because these three products all have “SQL” as part of their names, people occasionally get confused about the relationship between them and about which commands get executed where.
You can use rlwrap to add readline support to sqlplus. Run sqlplus like this:
$ rlwrap -c sqlplus username@database
Now up/down will scroll through command history. Use ctrl-r to search backwards through history, etc. This makes sqlplus bearable.
Also, add this to your login.sql to set the linesize to whatever the width of your terminal is:
HOST echo "set linesize" $(stty -a|head -n1|cut -f7 -d' '|cut -f1 -d';') > .tmp.sql
@.tmp.sql
HOST rm -f .tmp.sql
Both of these tips only work on unix.
Yes, I use SQL Plus every day in preference to Toad or SQL Developer (though I also use SQL Developer to browse the database).
I have the following in my login.sql script (which SQL Plus runs automatically):
1) Replace default editor (Notepad) with one of my choice:
define _editor = "C:\Program Files\TextPad 5\TextPad.exe"
2) Make SQL prompt show database name so I know where I am (thanks to Tom Kyte for this):
COLUMN global_name new_value gname
SET TERMOUT OFF
SELECT LOWER(USER) || '@' || global_name||CHR(10)||'SQL> ' AS global_name
FROM global_name;
SET SQLPROMPT '&gname'
SET TERMOUT ON
... plus other setting similar to yours.
I also find Tom Kyte's print_table procedure very useful.
Remember that we can put these settings in the login.sql script which will be run automatically whenever we start SQL*Plus. Find out more.
The neat thing about this is, that since 10g, this script is run every time we connect rather just the first time we fire up SQL*Plus...
SQL> conn apc
Enter password:
Connected.
Running login script
Session altered.
SQL> conn scott
Enter password:
Connected.
Running login script
Session altered.
SQL>
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
I like to use sqlplus in off-line.
sqlplus -S user/password @query.sql> file.txt
where query.sql is
set feedback off verify off heading off pagesize 0
...here goes a query...
quit;
/
So i can get info from the database in my bat/script files in windows or unix.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With