Professional Documents
Culture Documents
General SQL Scripts
General SQL Scripts
General SQL Scripts
SELECT job,
sum(decode(deptno,10,sal))
sum(decode(deptno,20,sal))
sum(decode(deptno,30,sal))
sum(decode(deptno,40,sal))
FROM scott.emp
GROUP BY job
DEPT10,
DEPT20,
DEPT30,
DEPT40
/
-----------
Sample output:
JOB
DEPT10
DEPT20
DEPT30
DEPT40
--------- ---------- ---------- ---------- ---------ANALYST
6000
CLERK
1300
1900
950
MANAGER
2450
2975
2850
PRESIDENT
5000
SALESMAN
5600
set serveroutput on
set veri off feed off
prompt Lookup Oracle error messages:
prompt
prompt Please enter error numbers as negatives. E.g. -1
prompt
exec dbms_output.put_line('==> '||sqlerrm( &errno ) );
set veri on feed on
undef errno
select banner
from
sys.v_$version;
select '
With the '||parameter||' option'
from
sys.v_$option
where value = 'TRUE';
select '
The '||parameter||' option is not installed'
from
sys.v_$option
where value <> 'TRUE';
begin
dbms_output.put_line('Port String: '||dbms_utility.port_string);
end;
/
set head on feed on
-------------
---------------
Example:
Given a table called emp with the following columns:
id
number
name varchar2(20)
sal number
For the second lowest salary:
select level, min(sal) from emp
where level=2
connect by prior sal < sal
group by level
-- drop table x
-- /
create table x (a char, b number default 99999, c date, d varchar2(6))
/
alter table x modify (c date default sysdate)
/
insert into x(a, d) values ('a', 'qwerty')
/
select * from x
/
--- Expected output:
--- A
B C
D
-- - ---------- ----------- ------- a
99999 25-APR-2001 qwerty
-
-- The following code tells the database what the application is up to:
begin
dbms_application_info.set_client_info('BANCS application info');
dbms_application_info.set_module('BANCS XYZ module', 'BANCS action
name');
end;
/
-- Retrieve application info from the database:
select module, action, client_info
from
sys.v_$session where audsid = USERENV('SESSIONID')
/
select sql_text
from
sys.v_$sqlarea
where module = 'BANCS XYZ module'
and action = 'BANCS action name'
/
select info
from
system.help
where upper(topic)=upper('&1')
/
select year,
decode( mod(year, 4), 0,
decode( mod(year, 400), 0, 'Leap Year',
decode( mod(year, 100), 0, 'Not a Leap Year', 'Leap Year')
), 'Not a Leap Year'
) as leap_year_indicator
from
my_table
/
SELECT TRANSLATE(
'HELLO WORLD',
-- Message to encode
'ABCDEFGHIJKLMNOPQRSTUVWXYZ ',
'1234567890!@#$%^&*()-=_+;,.') ENCODED_MESSAGE
FROM DUAL
/
SELECT TRANSLATE(
'85@@%._%*@4',
-- Message to decode
'1234567890!@#$%^&*()-=_+;,.',
'ABCDEFGHIJKLMNOPQRSTUVWXYZ ') DECODED_MESSAGE
FROM DUAL
/
set termout off echo off feed off trimspool on head off pages 0
spool countall.tmp
select 'SELECT count(*), '''||table_name||''' from '||table_name||';'
from
user_tables
/
spool off
set termout on
@@countall.tmp
set head on feed on
drop table x
/
create global temporary table x (a date)
on commit delete rows
-- Delete rows after commit
-- on commit preserve rows
-- Delete rows after exit session
/
select table_name, temporary, duration
from
where
/
user_tables
table_name = 'X'