Changing the prompt dynamically in SQL*Plus
This is just a simple trick which is not well-known but makes life easier when using the good ol’ SQL*Plus commandprompt (and many people still do!).
If you set the SQLPROMPT environment variabele (either manually or in the login.sql) the standard prompt is replaced by the name of the instance:
SQL> set sqlprompt "_CONNECT_IDENTIFIER>"
mydb>
I use this in combination with the login.sql. I’m not sure if this was a feature before Oracle 10g, but recently I noticed that the login.sql is executed after every login, and not only when you start a new prompt. And that means that everytime you change your connection, the sqlprompt also changes according to the value of _CONNECT_IDENTIFIER. This probably will prevent many mistakes because you’re typing update-statements in the "wrong" environment!
These substitution variables are available from SQL*Plus 9.2 onwards. Other values you can use for sqlprompt are _USER, _DATE, _O_VERSION and _O_RELEASE. They speak for themselves I guess!
mydb> set sqlprompt "_USER _CONNECT_IDENTIFIER> "<br />SCOTT mydb>
OK, that went wrong (now hopefully in the write order):
–
–
–
–
–
define uniqueness=”uniqueness”
column uniqueness new_value uniqueness
– ——————————————————————————-
set termout off
– Table dba_indexes has changed in Oracle 8
– Next piece of code switches the column name of the “create index” cursor.
select ‘decode(i.uniqueness,”BITMAP”,i.uniqueness,”NORMAL”)’ uniqueness
from dual where &&_O_RELEASE like ’7%’
union
select ‘i.index_type’ uniqueness
from dual where &&_O_RELEASE like ’8%’
;
– ——————————————————————————-
set termout on
set define ~
set serveroutput on size 1000000
–
–
–
–
– etc, etc, etc –
–
–
–
–
— Rebuild index
CURSOR c_index( p_index IN CHAR
, p_schema IN CHAR
)
IS
SELECT i.owner idxowner
, i.index_name
, ~uniqueness indextype
, decode(i.uniqueness, ‘UNIQUE’,’ UNIQUE ‘,’ ‘) uq
, i.table_name
, i.table_owner
, i.tablespace_name
, i.initial_extent
, i.next_extent
, i.min_extents
, i.max_extents
, i.pct_increase
, i.freelists
, i.pct_free
, decode(c.constraint_type,’P',’PRIMARY KEY’
,’U',’UNIQUE’
,c.constraint_type) constype
, c.constraint_name
, c.status
, c.owner consowner
FROM sys.dba_indexes i
, sys.dba_constraints c
WHERE (c.table_name(+) = i.table_name
AND c.constraint_name(+) = i.index_name
AND c.owner(+) = i.table_owner)
AND i.table_type = ‘TABLE’
AND i.index_name = p_index
AND i.owner = p_schema
ORDER BY i.index_name
;
–
–
–
–
–
–
– etc, etc, etc –
–
–
–
BEGIN
–
…etc…etc
–
–
–
IF check3(i).s_segment_type=’INDEX’
THEN
FOR r_index IN c_index(check3(i).s_segment_name,check3(i).s_owner)
LOOP
EXIT WHEN c_index%NOTFOUND;
create_check3(i).r_statement := ‘alter index ‘||r_index.idxowner||’.'||r_index.index_name
||chr(10)
||’REBUILD ONLINE’
||chr(10)
||’PCTFREE ‘||r_index.pct_free
||chr(10)
||’storage’
||chr(10)
||’(‘
||chr(10)
||’initial ‘||v_seg_initial||’K’
||chr(10)
||’next ‘||v_seg_nextext||’K’
||chr(10)
||’pctincrease ‘||r_index.pct_increase
||chr(10)
||’minextents ‘||r_index.min_extents
||chr(10)
||’maxextents ‘||v_max_extents
||chr(10)
||’freelists ‘||r_index.freelists
||chr(10)
||’)’
||chr(10)
||’tablespace ‘||r_index.tablespace_name
||’;’
;
dbms_output.put_line(create_check3(i).r_statement);
dbms_output.put_line(‘–’);
END LOOP;