Oracle ch3 Function
Oracle ch3 Function
1. Create a function.
2. Call a function.
3. Get information on functions.
4. Drop a function
2
CREATE [OR REPLACE] FUNCTION function_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])]
RETURN type
{IS | AS}
BEGIN
function_body
END function_name;
3
CREATE FUNCTION circle_area (
p_radius IN NUMBER) RETURN NUMBER AS
v_pi NUMBER := 3.1415926;
v_area NUMBER;
BEGIN
v_area := v_pi * POWER(p_radius, 2);
RETURN v_area;
END circle_area;
4
SELECT circle_area(2) FROM dual;
SELECT circle_area(p_radius => 2) FROM dual;
5
SELECT *
FROM user_procedures
WHERE object_name = 'CIRCLE_AREA'
6
DROP FUNCTION circle_area;
7
1. Viết hàm f_snt(n int) trả về kết quả n là số nguyên tố hay không.
8
CREATE or replace function songuyento(n INTEGER) return INTEGER
AS
nt integer :=1;
BEGIN
if n<2 then nt:=0; end if;
if n>2 then
for i in 2..n-1 loop
if n mod i =0 then nt:=0; end if;
end loop;
end if;
return nt;
END songuyento ;
declare n INTEGER:=&n;
begin
if songuyento(n)=1 then
DBMS_OUTPUT.PUT_LINE(n||'là số nguyên tố');
else
DBMS_OUTPUT.PUT_LINE(n||'không là số nguyên tố');
end if;
End; 9
PL/SQL Cursor Variables with REF CURSOR
10
Introduction to PL/SQL cursor variables
Declare a cursor variable
Examples
A cursor variable is a variable that references to a cursor.
It enables passing the result of a query between PL/SQL programs.
Strong typed REF CURSOR.
DECLARE
TYPE customer_t IS REF CURSOR RETURN customers%ROWTYPE;
c_customer customer_t;
Weak typed REF CURSOR.
DECLARE
TYPE customer_t IS REF CURSOR;
c_customer customer_t;
SYS_REFCURSOR is a predefined weak typed REF CURSOR
DECLARE
c_customer SYS_REFCURSOR;
Create the function returns a weak typed REF CURSOR variable