SQL*PlusThese are my personal notes that I use as a quick help in my work.
|
|
PROMPT a text
prompt =========================
PAUSE (Press ENTER to continue, ^C to stop, ^D in unix)
DESC table-name/proc-name
xyz -
continuation
"..."
'...'
[HKEY_LOCAL_MACHINE\Software\ORACLE]
"ORACLE_HOME"="D:\ORANT"
[HKEY_LOCAL_MACHINE\Software\ORACLE\ALL_HOMES]
"HOME_COUNTER"="1"
"DEFAULT_HOME"="DEFAULT_HOME"
"LAST_HOME"="0"
[HKEY_LOCAL_MACHINE\Software\ORACLE\HOME0]
"ID"="0"
"ORACLE_HOME"="D:\ORANT"
"ORACLE_HOME_NAME"="DEFAULT_HOME"
PATH=C:\PROGRA~1\MSOFFICE\OFFICE;C:\WINNT;C:\ORANT\BIN;
[HKEY_LOCAL_MACHINE\Software\ORACLE]
"TNS_ADMIN"="D:\ORANT\network\admin"
HEADING -->
xxxx xxxx <-COLSEP-> xxxx <-
HEADSEP
------ ----------------- ---- <-
UNDERLINE
nnn ttt <-COLSEP-> aaa <-
NULL, NUMFORMAT, NUMWIDTH
nnn ttt aaa <-
TRIMOUT, TRIMSPOOL
PAGESIZE, NEWPAGE, PAUSE-->
xxxx xxxx xxxx
------ ----------------- ----
nnn ttt aaa
nnn ttt ttt tt tttt
t aaa
ttt
<- WRAP
<-
RECSEP, RECSEPCHAR
nnn ttt aaa
10 rows selected. <-
FEEDBACK
BREAK ON col_name SKIP n DUP ON col_name2 SKIP m DUP
DUP
: show value on every line COMPUTE { AVG | COUNT | MAX | MIN | NUMBER | STD | SUM | VAR }
[ LABEL label_text ]
OF column name
ON { group_column_name | ROW | REPORT }
BREAK ON REPORT
COMPUTE SUM OF ... ON REPORT
--> grand total CLEAR COLUMNS
CLEAR BREAKS
COLUMN col_name HEADING hhh FORMAT fff
HEADING "line1|line2"
--> line break in heading
(see SET HEADSEP
) COLUMN col_name
COLUMN col_name CLEAR
CLEAR COLUMNS
SET COLSEP text
SET HEADING [ ON | OFF ]
NEWPAGE
for
the empty lines between pages.SET HEADSEP [ ON | OFF | char ]
SET UNDERLINE { ON | OFF | char]
SET FEEDBACK [ ON|OFF| n ]
SET NEWPAGE [ NONE | n ]
SET LINESIZE n
SET LOBOFFSET
SET LONG n
SET TAB [ OFF | ON ]
SET NEWPAGE { n | NONE }
NONE
for no lines and no FormFeed character (note that even with
NONE
, the heading will still show)
SET NUMFORMAT format_spec
SET NUMWIDTH n
SET PAGESIZE n
SET PAUSE { ON | OFF | pause_msg }
SET RECSEP { WRAPPED | EACH | OFF }
SET RECSEPCHAR
.SET RECSEPCHAR "char"
SET RECSEP
).
Default is space.SET TRIMOUT [ ON | OFF ]
SET TRIMSPOOL [ ON | OFF ]
SHOW BTITLE
SHOW TTITLE
SHOW lno
SHOW pno
SHOW REPHEADER
SHOW REPFOOTER
SET WRAP [ ON | OFF ]
STORE SET _current_settings.sql REPLACE
@_current_settings.sql
SET MARKUP HTML ON HEAD "<title>SQL*PLUS output</title>" BODY "bgcolor=#FFFFFF"
ENTMAP ON SPOOL ON PREFORMAT OFF
SPOOL GLU.HTML
-- whatever
SPOOL OFF
SET MARKUP HTML OFF
-- ENTMAP ON: Transforms the > and < into /gt; and <
-- PREFORMAT ON: Display with <PRE>
define the_log_short = &the_output_path.&the_file_name._short.log
define the_log_complete = &the_output_path.&the_file_name._complete.log
define the_log_temp = &the_output_path.&the_file_name._temp.log
spool &the_log_temp.
prompt ==================================================================
...
spool off
host cat &the_log_temp >> &the_log_complete
spool &the_log_temp.
...
spool off
host cat &the_log_temp >> &the_log_complete
host echo "Summary of data .... " >> &the_log_short
-- clean up
host rm &the_log_temp
Example:
COLUMN TODAY NEW_VALUE CURRENT_DATE NOPRINT COLUMN TIME NEW_VALUE CURRENT_TIME NOPRINT COLUMN DATABASE NEW_VALUE DATA_BASE NOPRINT COLUMN PASSOUT NEW_VALUE DBNAME NOPRINT COLUMN passout2 new_value user noprint REM TTITLE LEFT "Date: " current_date - --> Note that variables are without the ampersand CENTER "Daily report" - col 72 "Page:" format 999 SQL.PNO - --> use right instead SKIP 1 - --> Next line LEFT "Time: " current_time - CENTER "The report heading" - RIGHT format a15 SQL.USER - SKIP 1 - CENTER format a20 data_base - SKIP 2 REM REM SET heading off --> turned on further down SET pagesize 0 --> set to 58 further down REM SET termout off --> turned on further down SELECT TO_CHAR (SYSDATE, 'MM/DD/YY') today, TO_CHAR (SYSDATE, 'HH:MI AM') TIME, NAME || ' database' DATABASE, RTRIM (NAME) passout, USER passout2 FROM v$database; REM SET termout on SET heading on SET pagesize 58 SET newpage 0
set showmode off
set termout off feedback off verify off echo off newpage none heading
on underline off pagesize 40000 trimspool on
set colsep ";" null ""
clear breaks
spool xyz.csv
-- ... Put query here
spool off
set termout on feedback 6 newpage 1 heading on underline "-" colsep "
" null "*"
(Note: pagesize of 40'000 because Excel allows up to 36'000 and some
Note: pagesize of 0 removes an extra empty line at end of queries)
set pagesize 2000
Then format the columns with:
set linesize 120
column the_column_name format A20
column tab_char new_value tab_char
select chr(9) as tab_char from dual;
set colsep "&tab_char."
column this_year new_value this_year
column last_year new_value last_year
column two_years_ago new_value two_years_ago
column over_two_years_ago new_value over_two_years_ago
select 'yr_' || to_char( sysdate , 'YYYY') as this_year
, 'yr_' || to_char(add_months(sysdate, -12), 'YYYY') as last_year
, 'yr_' || to_char(add_months(sysdate, -24), 'YYYY') as two_years_ago
, 'yr_' || to_char(add_months(sysdate, -36), 'YYYY') || '_and_before' as over_two_years_ago
from dual;
column &this_year format 999G999G999G990
column &last_year format 999G999G999G990
column &two_years_ago format 999G999G999G990
column &over_two_years_ago format 999G999G999G990
select sum(case when trunc(the_date_field, 'YY')=trunc(sysdate , 'YY') then 1 end) as &this_year
, sum(case when trunc(the_date_field, 'YY')=trunc(add_months(sysdate, -12), 'YY') then 1 end) as &last_year
, sum(case when trunc(the_date_field, 'YY')=trunc(add_months(sysdate, -24), 'YY') then 1 end) as &two_years_ago
, sum(case when trunc(the_date_field, 'YY')<trunc(add_months(sysdate, -24), 'YY') then 1 end) as &over_two_years_ago
, to_char(the_date_field, 'YYYY')
from a_table
group by to_char(the_date_field, 'YYYY')
order by to_char(the_date_field, 'YYYY');
Note that column names must begin with an alphabetical character. I put "yr_" above.
SHOW ALL
SHOW setting
SET COPYCOMMIT n
SET COPYTYPECHECK {ON | OFF}
SET ECHO [ ON | OFF ]
SET SQLCONTINUE "> "
SET SQLNUMBER [ ON | OFF ]
SET SQLPREFIX char
SET SQLPROMPT "&the_db > "
SET TIME [ ON | OFF ]
SET SQLTERMINATOR [ ON | OFF | "char" ]
SET SQLBLANKLINES [ ON | OFF ]
SET BLOCKTERMINATOR
SET SERVEROUTPUT { ON | OFF } [ SIZE n ] [FORMAT { WRAPPED | WORD_WRAPPED
| TRUNCATED }]
SET SHOWMODE [ ON | OFF | BOTH ]
@filename
SET SUFFIX ".sql"
SET TERMOUT [ ON | OFF ]
SET TIMING [ ON | OFF ]
SET TRUNCATE [ ON | OFF ]
SET SQLCONTINUE "> " ???
11:38:42 the_prompt > |
WHENEVER SQLERROR CONTINUE;
-- command whose eventual error should not stop the script, such as drop statement
WHENEVER SQLERROR EXIT 1;
SET DEFINE { ON | OFF | prefix_char }
SET ESCAPE { ON | OFF | escape_char }
SET VERIFY { ON | OFF }
SET CONCAT { ON | OFF | concat_char }
DEFINE var_name = a_value
UNDEFINE var_name
DEFINE var_name
DEFINE
DEFINE var_name = &1
@a_script.sql "parameter with spaces"
define var_name = "&1"
ACCEPT var_name [ NUMBER | CHAR | DATE ]
[ FORMAT format_spec ]
[ DEFAULT default_value ]
[ PROMPT text | NOPROMPT ]
[ HIDE ]
UNDEFINE var_name var_name2
COLUMN current_acctg_date NEW_VALUE the_bal_date OLD_VALUE prev_date
set heading off termout off
COLUMN acctg_date NEW_VALUE the_bal_date NOPRINT
SELECT to_char(current_accounting_date, 'DD/MM/YYYY' ) as acctg_date FROM
parameters;
prompt The current accounting date is &the_bal_date
set heading on termout on
DEFINE the_file_name = glu
SPOOL &glu..txt
xxx
SPOOL OFF
BIND
Example:
DEFINE the_id = put-a-value-here
ACCEPT the_id CHAR DEFAULT &the_id PROMPT "Enter the ID (default &the_id ):
"
select * from the_table where id = &the_id ;
DEFINE the_user = whoever
DEFINE local_db = PRD1
ACCEPT pw char prompt "Enter password for &the_user on database &local_db.:
" HIDE
connect &the_user/&pw@&local_db
DEFINE like_object = &1
ACCEPT like_object CHAR DEFAULT &like_object PROMPT "Enter object name (default &like_object.). Add wildcard % as necessary: "
select owner, object_name, object_type
from all_objects
where object_name like UPPER('&like_object.')
order by object_name, object_type, owner;
DELACIL (Lpos LAST) --> DEL A C I L ( + L for positioning + last)
a text
c /old/new/
c /old
del b *
del last
del 2 {3 | * | last}
I text
I
L {b | last}
L {b | *} [{e | * | last}]
Bold highlights the part that has to be customized
|
|
Fill in the command file name (here: "whatever") and the command (here: "fc xyz_orig.txt xyz_new.txt")
|
|
|
-- Assuming this procedure:
CREATE OR REPLACE PROCEDURE myproc (
a IN VARCHAR2,
b IN VARCHAR2,
c IN OUT SYS_REFCURSOR
) AS
BEGIN
OPEN c FOR SELECT * ...;
DBMS_OUTPUT.PUT_LINE('string out length: '||to_char(length(the_message)));
END;
/
|
SHOW ERRORS { function_name | procedure_name | package_name | ...
}
SHOW ERRORS [owner.]object
SHOW PARAMETERS [parameter-name]
SHOW RELEASE
SHOW SGA
SHOW SPOOL
SHOW SQLCODE
SHOW USER
PASSWORD [user]
COPY FROM username/password@sid
[ TO username/password@sid] (not necessary if already connected
here)
{ APPEND | CREATE | INSERT | REPLACE }
destination_table [(column_list)]
USING select_statement
Example (in target database):
COPY FROM sch/pw@sid APPEND table_name_in_target USING SELECT * from table_in_source where ...;
APPEND
CREATE
INSERT
REPLACE
Oracle client installer sets language with regional settings values. Use REGEDIT.
Key name: HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\HOME0
Name: NLS_LANG
Value: AMERICAN_AMERICA.WE8ISO8859P1
Windows | UNIX |
---|---|
define the_output_path = dat\ |
define the_output_path = $HOME/dat/ |
pause ENTER or ^C to quit |
pause ENTER or ^D to quit |
host type &the_log_temp >> &the_log_complete |
host cat &the_log_temp >> &the_log_complete |
host del &the_log_temp |
host rm &the_log_temp |
|
|
See a file helpus.sql in the orant sub-directory tree
Sources: