Functions
Functions
DEFINITION:
Functions are the sub-programs that must return a value.
SYNTAX:
Create [or replace] function<function name> [(<list of parameters>)] return <datatype> is >local declaration> is begin . . . end;
create or replace function funst(pid int) return st.currstock%type is nm st.currstock%type; begin select currstock into nm from st where code=pid; return nm; declare add varchar2(20); begin add:=funstock(&pid); dbms_output.put_line(add); end; /
OUTPUT:
Enter a value for pid:100 Old 4: add:=funstock(&pid); New4: add:=funstock(100); 1000 PL/SQL statements successfully completed. 2.Write a function to display the distance for a given rid number
TABLE CREATION:
SQL>Select * from route; RID 101 102 103 RNO 2 4 6 ORIGIN chennai chennai tirchy DESTN madurai blore chennai FARE 180 250 190 DIST 250 300 200
SCRIPT:
create or replace function rev(rid1 number) return route.dist%type is dist1 route.dist%type; begin select dist into dist1 from route where rid=rid1; return dist1; end; /
FUNCTION EXECUTION
declare rid2 number; begin rid2:=rev(&rid1); dbms_output.put_line(rid2); end; /
OUTPUT:
Function created. Enter value for rid1: 103 old 4: rid2:=rev(&rid1); new 4: rid2:=rev(103); PL/SQL procedure successfully completed.