Unit 3(rdbms)

17
UNIT : 3 APPLICATION DEVELOPMENT USING PROCEDURAL SQL

description

 

Transcript of Unit 3(rdbms)

Page 1: Unit 3(rdbms)

UNIT : 3 APPLICATION

DEVELOPMENT USING PROCEDURAL SQL

Page 2: Unit 3(rdbms)

CONTENT : Cursors : OPEN CLOSE and FETCH Creation of User Defined Function Creation of Stored Procedure

Page 3: Unit 3(rdbms)

CURSOR Types of cursor Cursor Predicates Referencing cursor variable Code

Page 4: Unit 3(rdbms)

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;

Page 5: Unit 3(rdbms)

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;

Page 6: Unit 3(rdbms)

EXAMPLE(SIMPLE) create or replace function

simple_function() returns varchar(10) begin return 'hello';

end; ;

Page 7: Unit 3(rdbms)

EXAMPLE Create or replace function bonus(salary

int, bonus int)returns int

return salary*bonus/100

Example

Page 8: Unit 3(rdbms)

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;

Page 9: Unit 3(rdbms)

RETURN TABLE Function also used to return table.

Code Function_get.txt

Page 10: Unit 3(rdbms)

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.

Page 11: Unit 3(rdbms)

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)

Page 12: Unit 3(rdbms)

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.

Page 13: Unit 3(rdbms)

STRUCTURE OF SPCreate or replace procedure procedure_name

(in | out | inout arg1 type1, in | out | inout arg2 type2,………)

Language SQL contains SQLBegin

……statement…..End

Page 14: Unit 3(rdbms)

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.

Page 15: Unit 3(rdbms)

Language SQL contains sqlOption can be

Reads SQL data Contains SQL data Modifies SQL data

Begin ….End statementLogic of store procedure

Code

Page 16: Unit 3(rdbms)

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@

Page 17: Unit 3(rdbms)

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