UNIT : 3APPLICATION 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 spe...
SyntaxCreate or replace function function_name (var1  datatype,var2 datatype) returns datatypelanguage SQL contains|reads|...
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/...
Example ( modifies data)create or replace function create_table() returns intlanguage sql modifies sql databegin  create t...
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 c...
Characteristics :   Support procedural construct in high level PL like C,    c++ and java etc.   Are stored in databases...
Benefits of SP   Reduce network usage between client and server.   Enhance capabilities       Increase memory disk spac...
Structure of SPCreate or replace procedure procedure_name  (in | out | inout arg1 type1,  in | out | inout arg2 type2,………)...
Section of SP:   Create procedure name:     It   define name of store procedure.     What    the name and data types of...
   Language SQL contains sql     Option   can be       Reads SQL   data       Contains   SQL data       Modifies SQL ...
Example   Codecreate or replace procedure get_update(IN eno int, in sal int)LANGUAGE SQL modifies sql databegin    update...
Unit Question   What is the use of in keyword?   What is the difference between function and    procedure?   What is th...
Practical example   empBranch ( empno, designation, basic_sal, DOB,    B_code)   Branch( B_code, city)   Write a proced...
ac_no      status   opndt   typeExample :                         101        S        10-3-11 Saving                      ...
Upcoming SlideShare
Loading in …5
×

Unit 3

747 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
747
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Unit 3

  1. 1. UNIT : 3APPLICATION DEVELOPMENT USING PROCEDURAL SQL Prepared By : Preeti Bhatt
  2. 2. Content :  Cursors : OPEN CLOSE and FETCH  Creation of User Defined Function  Creation of Stored Procedure
  3. 3. Cursor code
  4. 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. 5. SyntaxCreate or replace function function_name (var1 datatype,var2 datatype) returns datatypelanguage SQL contains|reads|modifies sql dataBegin ………SQL PL code……….. return <value>;End;
  6. 6. Example(simple)create or replace function simple_function() returns varchar(10) begin return hello; end; ;
  7. 7. Example Create or replace function bonus(salary int, bonus int) returns int return salary*bonus/100Example
  8. 8. 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;
  9. 9. Return Table Function also used to return table. code
  10. 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. 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. 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. 13. Structure of SPCreate or replace procedure procedure_name (in | out | inout arg1 type1, in | out | inout arg2 type2,………)Language SQL contains SQLBegin ……statement…..End
  14. 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. 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. 16. 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);
  17. 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. 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. 19. 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.

×