Вы находитесь на странице: 1из 3

Functions Usage : It is used for performing the calculations.. and evaluating an expr..

Note : * Functions can be executed from ->A select statement ->SQL Prompt ->Anonymous and Named Blocks.. Whenever the user defined functions are used in a select statement these functions will act as a single row expr.. and as a result they will return a single value per row retrieved by that statement.. Restrictions on Functions which are targeted to be used in a select statement. : -> Datatypes used in a function should be only oracle server specific datatypes. . -> DML Operations and TCL are restricted in a function if it is targetted to a Select statement. -> Return value is must in a function... even in the exception handling Statements.. -> Output statements return in a function will not work if it used in a select Statement.. --> Syntax : create [or replace] function function_name [( arg [in | out | in out] [nocopy] <datatype> ) ( arg [in | out | in out] [nocopy] <datatype> ) ] return <datatype> is / as --local variable declarations <begin> --executable statement(s) return <return_value>; [exception] --exception handling statement(s) <end> [function_name]; --> Create or replace function addnum ( a number, b number ) return number is begin return a+b; end; --> Executing function from select statement : -> -> Select addnum(10,20) from dual; Select empno,ename,sal,deptno,comm, addnum( sal, nvl(comm,0)) as "Total Salary" from emp

Note : The information about the stored procedures can be retrieved from user_objects.. --> Select object_name , object_type ,status from user_objects where object_name like 'ADDNUM' The source of the stored procedures can be retrieved from user_source --> Select text from user_source[all_source][, dba_source] where name like 'ADDNUM'; --> Create or replace function getannsal ( eno number ) return number is tsal number; begin Select sal into tsal from emp where empno = eno; return tsal*12; end; --> Select empno,ename,sal,deptno, getannsal(empno) as "Annual Salary" from emp --> Executing the functions from the SQL Prompt : -> Declare a bind variable variable annsal number -> Execute the function : Syntax : exec :bind_variable := function_name( [arg_value(s)] ) exec :annsal := getannsal( 7788 ); -> Print the value of the bind variable : Syntax: print variable_name ex : print annsal --------------------------------------------------------------------------------------------

Exceptions : These are the runtime errors which are raised based on the invalid value(s) prov ided by the user.. at the runtime.. create or replace function divnum ( a number, b number ) return number is

begin return a/b; exception when zero_divide then dbms_output.put_line( 'Oops!.. cant divide number by Zero... ' ); end; declare a number := &a; b number := &b; res number; begin res := divnum(a , b ); dbms_output.put_line( 'Result : ' || res ); end; create or replace function getsalary ( name emp.ename%type ) return emp.sal%type is tsal emp.sal%type; begin select sal into tsal from emp where ename like name; return tsal; exception when no_data_found then dbms_output.put_line( 'Oops!.. Employee doesnot exists..' ); return -1; when too_many_rows then dbms_output.put_line( 'I OOOO... i got confused too many records found...' ); return -2; end; variable salary number; exec :salary := getsalary( 'SMITH' ); print salary exec :Salary := getsalary( 'USHA' ); select empno,ename,getsalary( ename ) as "Sal" from emp select getsalary('RAMIYA') from dual;