Download - Unit 3(rdbms)
UNIT : 3 APPLICATION
DEVELOPMENT USING PROCEDURAL SQL
CONTENT : Cursors : OPEN CLOSE and FETCH Creation of User Defined Function Creation of Stored Procedure
CURSOR Types of cursor Cursor Predicates Referencing cursor variable 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;
SYNTAXCreate or replace function function_name (var1
datatype,var2 datatype) returns datatype language 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/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 Function_get.txt
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.
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. 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.Out – output variable. inout – both input and output.
Language SQL contains sqlOption can be
Reads SQL data Contains SQL data Modifies SQL data
Begin ….End statementLogic of store procedure
Code
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@
CREATE PROCEDURE ADMINISTRATOR.UPDATE_SAL (IN empNum CHAR(6), IN rating SMALLINT)
LANGUAGE SQL BEGIN IF rating = 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