0% found this document useful (0 votes)
17 views

Functions

1. Functions are sub-programs that must return a value. The syntax to create a function includes specifying the function name, list of parameters, and return data type. 2. A function is executed using the syntax of a declaration part followed by a begin and end statement containing PL/SQL statements. 3. Sample coding is provided to create a function that searches the stock table to return the current stock using an item code, and another function to return the distance from the route table using a ride ID number.

Uploaded by

Aravind Ram
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Functions

1. Functions are sub-programs that must return a value. The syntax to create a function includes specifying the function name, list of parameters, and return data type. 2. A function is executed using the syntax of a declaration part followed by a begin and end statement containing PL/SQL statements. 3. Sample coding is provided to create a function that searches the stock table to return the current stock using an item code, and another function to return the distance from the route table using a ride ID number.

Uploaded by

Aravind Ram
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

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;

FUNCTION EXECUTION: SYNTAX:


<declaration part> begin PL/SQL statements end;

SAMPLE CODING: PROGRAM STATEMENT:


1.Write the function to search for obtaining the current stock of an item using the item code.

SCRIPT: Table creation:


SQL>Create table stock(code number,currstock number); Table created SQL>select * from stock;

Code 100 200

currstock 1000 2000

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.

You might also like