The document discusses different procedural SQL concepts like cursors, user defined functions, and stored procedures. It provides examples of creating functions that return values and tables, as well as procedures that update data. The examples demonstrate how to define parameters, return values, and write PL/SQL code within functions and procedures to perform tasks like updating records and returning results.
4. Creating User Defined Function
Function : A function is a logical grouped set of SQL/PL
statement that perform a specific task.
PL/SQL functions are created by executing the CREATE
FUNCTION statement.
Such functions can be dropped from the database by using
the DB2 SQL DROP statement.
Drop function Func_name;
5. Syntax
Create or replace function function_name (var1
datatype,var2 datatype) returns datatype
language SQL contains|reads|modifies sql data
Begin
………SQL PL code………..
return <value>;
End;
7. Example
Create or replace function bonus(salary int, bonus
int)
returns int
return salary*bonus/100
Example
8. Example ( modifies data)
create or replace function create_table() returns int
language sql modifies sql data
begin
create table st(id int, name varchar(10), salary numeric(10));
return 0;
end;
9. Return Table
Function also used to return table.
code
10. Stored Procedure
Stored Procedure is a logical grouped set of SQL/PL
statement that perform a specific task.
It can help to improve application performance and
reduce database access traffic.
Procedure does not return any value.
11. Characteristics :
Support procedural construct in high level PL like C,
c++ and java etc.
Are stored in databases and run on DB2 server.
Easily call whenever required(call statement)
12. Benefits of SP
Reduce network usage between client and server.
Enhance capabilities
Increase memory disk space on the server
Improve security
User can call stored procedure but do not required direct
access to the content of the database .
Reduced development cost.
Centralized security and maintenance.
13. Structure of SP
Create or replace procedure procedure_name
(in | out | inout arg1 type1,
in | out | inout arg2 type2,………)
Language SQL contains SQL
Begin
……statement…..
End
14. Section of SP:
Create procedure name:
It define name of store procedure.
What the name and data types of argument.
In – input variable.(Read only variable)
Out – output variable.(write only variable)
inout – both input and output.(read and write)
15. Language SQL contains sql
Option can be
Reads SQL data
Contains SQL data
Modifies SQL data
Begin ….End statement
Logic of store procedure
16. Example
Code
create or replace procedure get_update(IN eno int, in sal int)
LANGUAGE SQL modifies sql data
begin
update empPr set salary=sal where id=eno;
end;
Call get_update(3,20000);
17. Unit Question
What is the use of in keyword?
What is the difference between function and
procedure?
What is the use of contains, reads and modify data in
function or procedure?
What is function? Explain with example.
What is procedure? Explain with example.
18. Practical example
empBranch ( empno, designation, basic_sal, DOB,
B_code)
Branch( B_code, city)
Write a procedure to give 10% bonus to all
managers from ‘Ahemdabad’ branch.
19. ac_no status opndt type
Example : 101 S 10-3-11 Saving
102 S 12-4-11 current
103 S 10-2-12 saving
Table :
acct_mstr ( ac_no, status, opndt, type )
Trans_mstr( trans_no, ac_no, dt,a_type,t_type,amt,balance)
Inactive_acct (ac_no, opndt, type)
Bank manager has decided to mark all those account as inactive(I)
on which there are no transactions performed in last 365 days.
Whenever any update takeplace,a record for same is maintained in
inactive_acct table.
Write plsql block using cursor to do same.