pl sql-function
-
Upload
bhaskar1234567 -
Category
Documents
-
view
213 -
download
0
Transcript of pl sql-function
-
7/29/2019 pl sql-function
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 thesefunctions will act as a single row expr.. and as a result they will return asingle 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 aSelect statement.
-> Return value is must in a function... even in the exception handlingStatements..
-> Output statements return in a function will not work if it used in a selectStatement..
--> Syntax :
create [or replace] function function_name[( arg [in | out | in out] [nocopy] )( arg [in | out | in out] [nocopy] ) ]
return is / as
--local variable declarations--executable statement(s)return ;[exception]--exception handling statement(s) [function_name];
--> Create or replace function addnum( a number, b number )return number
isbegin
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
-
7/29/2019 pl sql-function
2/3
Note : The information about the stored procedures can be retrievedfrom user_objects..
--> Select object_name , object_type ,statusfrom user_objectswhere 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 numberistsal number;beginSelect sal into tsal from empwhere 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) provided by the user.. at the runtime..
create or replace function divnum( a number, b number ) return numberis
-
7/29/2019 pl sql-function
3/3
beginreturn a/b;exceptionwhen zero_divide thendbms_output.put_line( 'Oops!.. cant divide number by Zero... ' );end;
declarea number := &a;b number := &b;res number;beginres := divnum(a , b );dbms_output.put_line( 'Result : ' || res );end;
create or replace function getsalary( name emp.ename%type )return emp.sal%typeistsal emp.sal%type;beginselect sal into tsal from empwhere ename like name;
return tsal;exceptionwhen no_data_found thendbms_output.put_line( 'Oops!.. Employee doesnot exists..' );return -1;when too_many_rows thendbms_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;