PL/SQL procedure successfully completed.
PL/SQL da funksiyalar bilan ishlash. Funksiyalar
protseduralardan
farqli ravishda natijani qaytaruvchi xossaga ega bo‘ladi. Funksiyani e’lon
qilish:
CREATE [OR REPLACE] FUNCTION function_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])]
RETURN return_datatype
{IS | AS}
BEGIN
< function_body >
END [function_name];
Misol.
CREATE OR REPLACE FUNCTION totalCustomers
RETURN number IS
total number(2) := 0;
BEGIN
SELECT count(*) into total
FROM customers;
RETURN total;
END;
Funsiya’ni chaqirish
DECLARE
c number(2);
BEGIN
c := totalCustomers();
DBMS_OUTPUT.PUT_LINE('Total no. of Customers: ' || c);
END;
Misol. Ikkita sondan kattasini aniqlash funksiyasi
DECLARE
a number;
b number;
c number;
FUNCTION findMax(x IN number, y IN number)
RETURN number
IS
z number;
BEGIN
IF x > y THEN
z:= x;
ELSE
Z:= y;
END IF;
RETURN z;
END;
BEGIN
a:= 23;
b:= 45;
c := findMax(a, b);
DBMS_OUTPUT.PUT_LINE(' Maximum of (23,45): ' || c);
END;