PL/SQL Quick Reference Program structure DECLARE | Create

advertisement
PL/SQL Quick Reference
Program structure
DECLARE | Create (procedure | function)
variables definitions
BEGIN
procedural statements
exceptions
END
Package structure:
Create or replace package
package_name
declarations;
program specifications;
END
Package Body:
Create or replace package body
package_name
declarations;
program specifications;
BEGIN
. .
END;
Exception Handling
EXCEPTION
WHEN exception_name1 THEN
[statements]
WHEN OTHERS THEN
[statements]
END [procedure_name];
when rowtype_mismatch then
dbms_output.put_line(‘Bad Errors’);
when others then
raise_application_error
(-20001,'An error - '||SQLCODE||' '||SQLERRM);
Date functions:
Add_months
last_day
Months_between
next_day
sysdate
trunc
Function prototype:
create or replace function
plus_tax(v_msrp in number) return number
as
v_total number;
begin
v_total := v_msrp + (v_msrp * .07);
return v_total;
end;
/
select price, plus_tax(price) from
titles;
Implicit cursor:
begin
for r_dept in
(select store_name, sales from store)
loop
do all that fun stuff.
end loop;
end;
Explicit Cursor:
CURSOR c_data IS
SELECT description
FROM
sql_test;
BEGIN
OPEN c_data;
LOOP
FETCH c_data
INTO l_description;
EXIT WHEN c_data%NOTFOUND;
DBMS_OUTPUT.put_line('l_description=' ||
l_description);
END LOOP;
CLOSE c_data;
PL/SQL Quick Reference
Conversion functions
nvl
to_char
to_date
to_number
Looping statements
LOOP
...
EXIT . . . WHEN
END LOOP;
Exceptions:
FOR I in lower_bound ..upper_bound
LOOP
....
END LOOP;
ORA-06530
access_into_null
ORA-06592
case_not_found
ORA-06531
collection_is_null
ORA-06511
cursor_already_open
ORA-00001
dup_val_on_index
ORA-01001
invalid_cursor
ORA-01722
invalid_number
ORA-01017
login_denied
ORA-01403
no_data_found
ORA-01012
not_logged_on
ORA-06501
program_error
ORA-06504
rowtype_mismatch
ORA-30625
self_is_null
ORA-06500
storage_error
subscript_beyond_count ORA-06533
subscript_outside_limit ORA-06532
ORA-01410
sys_invalid_rowid
ORA-00051
timeout_on_resource
ORA-01422
too_many_rows
ORA-06502
value_error
ORA-01476
zero_divide
While expression
LOOP
....
END LOOP;
Boolean operators
if expression 1 then
..
ELSIF expression 2 then
ELSIF expression 3 then
ELSE
END IF:
CASE
when expression 1 then
when expression 2 then
ELSE . . .
END CASE;
Table join Loop:
cursor c1 is select
price
from titles;
title_id,
cursor c2 is select
title_id,
sum_qty
from sales
where
title_id = v_title_id
group by
title_id;
title,
sum(qty)
begin
open c1;
fetch c1 into v_title_id, v_title, v_price;
while c1%FOUND
loop
open c2;
fetch c2 into v_title_id2, v_sum_qty;
dbms_output.put_line(v_title_id||'
'||v_price||' '||v_price*v_sum_qty);
close c2;
fetch c1 into v_title_id, v_title, v_price;
end loop;
SELECT last_name, commission_pct,
(CASE commission_pct
WHEN 0.1 THEN ‘Low’
WHEN 0.15 THEN ‘Average’
WHEN 0.2 THEN ‘High’
ELSE ‘N/A’
END ) Commission
FROM employees ORDER BY last_name;
UTL_FILE
Write to a file:
alter system set utl_file_dir = ‘:alert_log’);
utl_file.fopen(':alert_loc',’alertprod.log’,'W');
dbms_output.put_line('invalid_application_error');
utl_file.fclose(':alert_loc');
Download