Unit 3(rdbms)


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Unit 3(rdbms)

  1. 1.  Cursors : OPEN CLOSE and FETCH Creation of User Defined Function Creation of Stored Procedure
  2. 2.  Types of cursor Cursor Predicates Referencing cursor variable Code
  3. 3.  Function : A function is a logical grouped set of SQL/PL statement that perform a specific task. PL/SQL functions are created by executing theCREATE FUNCTION statement. Such functions can be dropped from the database by using the DB2 SQL DROP statement.  Drop function Func_name;
  4. 4. Create or replace function function_name (var1 datatype,var2 datatype) returns datatypelanguage SQL contains|reads|modifies sql dataBegin ………SQL PL code……….. return <value>;End;
  5. 5. create or replace function simple_function() returns varchar(10) begin return hello; end; ;
  6. 6.  Create or replace function bonus(salary int, bonus int) returns int return salary*bonus/100Example
  7. 7. 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;
  8. 8.  Function also used to return table. Code Function_get.txt
  9. 9.  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.
  10. 10.  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)
  11. 11.  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. Reduced development cost. Centralized security and maintenance.
  12. 12. Create or replace procedure procedure_name (in | out | inout arg1 type1, in | out | inout arg2 type2,………)Language SQL contains SQLBegin ……statement…..End
  13. 13.  Create procedure name:  It define name of store procedure.  What the name and data types of argument.  In – input variable.  Out – output variable.  inout – both input and output.
  14. 14.  Language SQL contains sql  Option can be  Reads SQL data  Contains SQL data  Modifies SQL data Begin ….End statement  Logic of store procedure Code
  15. 15.  Create Procedure p() BEGIN DECLARE C1 cursor; DECLARE varInt INT; SET count = -1; SET c1 = CURSOR FOR SELECT c1 FROM t1; IF (c1 IS NOT OPEN) THEN OPEN c1; ELSE set count = -2; END IF; set count = 0; IF (c1 IS OPEN) THEN FETCH c1 into varInt; WHILE (c1 IS FOUND) DO SET count = count + 1; FETCH c1 INTO varInt; END WHILE; ELSE SET count = 0; END IF; END@
  16. 16.  CREATE PROCEDURE ADMINISTRATOR.UPDATE_SAL (IN empNum CHAR(6), IN rating SMALLINT) LANGUAGE SQL BEGIN IFrating = 1 THEN UPDATE employee SET salary = salary * 1.10, bonus = 1500 WHERE empno = empNum; ELSE UPDATE employee SET salary = salary * 1.05, bonus = 1000 WHERE empno = empNum; END IF; END