Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

Oracle ch3 Function

1. A cursor variable is a variable that references a cursor and allows passing query results between PL/SQL programs. 2. Cursor variables can be strongly typed using %ROWTYPE or weakly typed using SYS_REFCURSOR. 3. A function can return a weakly typed REF CURSOR variable containing the results of a query, which an anonymous block can then call and process.

Uploaded by

Tài Tấn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Oracle ch3 Function

1. A cursor variable is a variable that references a cursor and allows passing query results between PL/SQL programs. 2. Cursor variables can be strongly typed using %ROWTYPE or weakly typed using SYS_REFCURSOR. 3. A function can return a weakly typed REF CURSOR variable containing the results of a query, which an anonymous block can then call and process.

Uploaded by

Tài Tấn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

1

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;

Datatypes cannot specify length, precision, or


scale.

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

CREATE OR REPLACE FUNCTION get_direct_reports(


in_manager_id IN employees.manager_id%TYPE)
RETURN SYS_REFCURSOR
AS
c_direct_reports SYS_REFCURSOR;
BEGIN
OPEN c_direct_reports FOR
SELECT employee_id, first_name, last_name, email
FROM employees
WHERE manager_id = in_manager_id
ORDER BY first_name, last_name;
RETURN c_direct_reports;
END;
 Anonymous block calls the get_direct_reports()
DECLARE
c_direct_reports SYS_REFCURSOR;
l_employee_id employees.employee_id%TYPE;
l_first_name employees.first_name%TYPE;
l_last_name employees.last_name%TYPE;
l_email employees.email%TYPE;
BEGIN
-- get the ref cursor from function
c_direct_reports := get_direct_reports(46);
-- process each employee
LOOP
FETCH c_direct_reports
INTO l_employee_id, l_first_name, l_last_name, l_email;
EXIT WHEN c_direct_reports%notfound;
dbms_output.put_line(l_first_name || ' ' || l_last_name || ' - ' || l_email );
END LOOP;
CLOSE c_direct_reports;
END;

You might also like