--19-1: Create a function with no parameters create OR REPLACE function cur_datetime return VARCHAR2 are BEGIN return To_char (sysdate, ' YYYY ' year ' MM ' month ' DD
"Day" Hh24″ when "MI" "SS" Seconds "');
End;
/--19-2: Create a function with input parameters create OR REPLACE function get_sal (name VARCHAR2) return number as V_sal Emp.sal%type;
BEGIN SELECT sal into v_sal from EMP WHERE Upper (ename) =upper (name);
return v_sal;
End; /--19-3: Create functions with output parameters create OR REPLACE function Get_info (Eno number,title out VARCHAR2) return VARCHAR2 as name Emp.enam
E%type;
BEGIN SELECT ename,job into Name,title from emp WHERE Empno=eno;
return name;
End; /--19-4: Create a function with input and output parameters create OR REPLACE function Get_upd_info (Eno number,sal_chg in Out number) return VARCHAR2 as name
Emp.ename%type;
BEGIN UPDATE emp SET sal=sal+sal_chg WHERE Empno=eno returning ename,sal into Name,sal_chg;
return name;
End; /--19-5: Create result cache function Create OR REPLACE function get_name (no VARCHAR2) return number Result_cache relies_on (EMP) as V_name E
Mp.ename%type; BEGIN SELECT ename into V_name from emp WHERE empno=nO
return v_name;
End;
/--19-6: Call function with no parameters BEGIN Dbms_output.put_line (cur_datetime);
End; /--19-7: Call function with input parameter BEGIN dbms_output.put_line (' Salary: ' | |
Get_sal (' &name '));
End;
/--19-8: Call functions with output parameters DECLARE V_name Emp.ename%type;
V_job Emp.job%type;
BEGIN V_name:=get_info (&eno,v_job); Dbms_output.put_line (' Name: ' | | v_name| | ', post: ' | |
V_job);
End;
/--19-9: Call functions with input and output parameters DECLARE V_empno Emp.empno%type;
V_name Emp.ename%type;
V_salchg Emp.sal%type;
BEGIN v_empno:=&eno;
v_salchg:=&incre;
V_name:=get_upd_info (V_EMPNO,V_SALCHG); Dbms_output.put_line (' Name: ' | | v_name| | ', new salary: ' | |
V_SALCHG);
End;
/--19-10: Use position pass for parameter pass variables and data SELECT get_sal (' &name ') payroll from dual; --19-11: Use name pass for parameter pass variables and data var salary number exec:salary:=get_sal (name=> ' &name ')--19-12: Passing variables and data var using combination passes for parameters Name VARCHAR2 VAR sal_chg number exec:sal_chg:=200 exec:name:=get_upd_info (&ENO,:SAL_CHG) PRINT name Sal_chg-
19-13: Call the Pl/sql function in the SQL statement SELECT get_sal (name=> ' Scott ') salary from dual; --19-14: Using exception handling creatE OR REPLACE FUNCTION get_sal (name VARCHAR2) return number as V_sal Emp.sal%type;
BEGIN SELECT sal into v_sal from EMP WHERE Upper (ename) =upper (name);
return v_sal;
EXCEPTION when No_data_found THEN raise_application_error (-20000, ' the employee does not exist ');
End;
/--19-15: Use record type as return type CREATE OR REPLACE FUNCTION get_info (ENO number) returns Emp%rowtype is Emp_record Emp%rowtype;
BEGIN SELECT * into Emp_record from emp WHERE Empno=eno;
return Emp_record;
EXCEPTION when No_data_found THEN raise_application_error (-20000, ' the employee does not exist ');
End;
/DECLARE Emp_record Emp%rowtype;
BEGIN Emp_record:=get_info (&eno); Dbms_output.put_line (' Name: ' | | emp_record.ename| | ', Department No.: ' | |
EMP_RECORD.DEPTNO);
End;
/--19-16: Use collection type as return type CREATE OR REPLACE type Ename_table_type is Table of VARCHAR2 (10);
/CREATE OR REPLACE FUNCTION get_name (DNO number) return ename_table_type is ename_table ename_table_type;
BEGIN SELECT ename BULK COLLECT into ename_table from emp WHERE Deptno=dno;
return ename_table; EXCEPTION when no_data_found theN Raise_application_error (-20099, ' the sector does not exist ');
End;
/DECLARE ename_table Ename_table_type;
BEGIN Ename_table:=get_name (&dno); For I in 1..ename_table. COUNT LOOP dbms_output.put_line (' Name: ' | |
Ename_table (i));
End LOOP;
End;
/--19-17: Delete function drop functions get_name; --19-18: Display compilation errors Show ERRORS-19-19: Determine function status SELECT object_name from user_objects WHERE status= ' INVALID ' and object_type= ' F
Unction ';
--19-20: The compiler functions ALTER function Get_info COMPILE;
--19-21: View function code SELECT text from User_source WHERE name= ' get_info ';