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;
SyntaxCreate or replace function function_name (var1 datatype,var2 datatype) returns datatypelanguage SQL contains|reads|modifies sql dataBegin ………SQL PL code……….. return <value>;End;
Example(simple)create or replace function simple_function() returns varchar(10) begin return hello; end; ;
Example Create or replace function bonus(salary int, bonus int) returns int return salary*bonus/100Example
Example ( modifies data)create or replace function create_table() returns intlanguage sql modifies sql databegin create table st(id int, name varchar(10), salary numeric(10)); return 0;end;
Return Table Function also used to return table. code
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.
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)
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.
Structure of SPCreate or replace procedure procedure_name (in | out | inout arg1 type1, in | out | inout arg2 type2,………)Language SQL contains SQLBegin ……statement…..End
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)
Language SQL contains sql Option can be Reads SQL data Contains SQL data Modifies SQL data Begin ….End statement Logic of store procedure
Example Codecreate or replace procedure get_update(IN eno int, in sal int)LANGUAGE SQL modifies sql databegin update empPr set salary=sal where id=eno;end;Call get_update(3,20000);
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.
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.
ac_no status opndt typeExample : 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.