UNIT : 3
APPLICATION DEVELOPMENT
 USING PROCEDURAL SQL



             Prepared By : Preeti Bhatt
Content :

    Cursors : OPEN CLOSE and FETCH

    Creation of User Defined Function

    Creation of Stored Procedure
Cursor
   code
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;
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;
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/100

Example
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;
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 SP

Create or replace procedure procedure_name
  (in | out | inout arg1 type1,
  in | out | inout arg2 type2,………)
Language SQL contains SQL
Begin
  ……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
   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);
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   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.

Unit 3

  • 1.
    UNIT : 3 APPLICATIONDEVELOPMENT USING PROCEDURAL SQL Prepared By : Preeti Bhatt
  • 2.
    Content :  Cursors : OPEN CLOSE and FETCH  Creation of User Defined Function  Creation of Stored Procedure
  • 3.
  • 4.
    Creating User DefinedFunction  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 replacefunction function_name (var1 datatype,var2 datatype) returns datatype language SQL contains|reads|modifies sql data Begin ………SQL PL code……….. return <value>; End;
  • 6.
    Example(simple) create or replacefunction simple_function() returns varchar(10) begin return 'hello'; end; ;
  • 7.
    Example  Create or replace function bonus(salary int, bonus int) returns int return salary*bonus/100 Example
  • 8.
    Example ( modifiesdata) 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 Createor 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.