Aplicationdatabase
Aplicationdatabase
Aplicationdatabase
Development
Overview
Concepts covered in this lecture:
SQL in application code
Embedded SQL
Cursors
Dynamic SQL
JDBC
SQLJ
Stored procedures
Stored procedure
It is often important to execute some parts
of the application directly in the process
space of Database system.
Why?
3. To minimize data transferring between
database server and its client issuing SQL
statements.
4. To utilize the full power of the Database
server.
Sudarshan 3
When SQL statements are issued from a
remote application.
Sudarshan 4
What’s happen exactly ?
When SQL statements are issued from a
remote application.
The records in the result of query need to be transferred from
Database system back to the application.
– For that we can make use of cursors.
DBMS has resources:
1. Locks
2. Memory tied up-
while application is processing the records retrieved
through the cursor.
Is there any other way to avoid this?
Sudarshan 5
Stored Procedures
What is a stored procedure:
– Program executed through a single
SQL statement
– Executed in the process space of the
server.
– Or-
Sudarshan 7
Stored Procedure Language
Things to remember
2. can also write stored procedures using languages
such as C or Java
3. The more SQL statements that are grouped
together for execution, the larger the savings in
network traffic.
Sudarshan 8
Normal Database
Sudarshan 9
Applications using
stored procedures
Sudarshan 10
Creating simple stored Procedure
Eg: CREATE PROCEDURE showreserved
SELECT S.sid,sS.sname,count(*)
FROM sailors S ,Reserve R
WHERE S.Sid=R.sid
GROUP BY S.sid
Showreserved is the procedure name,which is
containing an SQL statement that is
precompiled and stored at the server .
Allows parameters local variables), loops.
Sudarshan 11
parameters
Stored procedures can have parameters:
Three different modes:
IN: parameters are arguments to the stored
procedures.
OUT: parameters are arguments from the
stored procedures.
INOUT: both
CREATE PROCEDURE
IncreaseRating
(IN sailor_sid INTEGER, IN increase INTEGER)
UPDATE Sailors SET rating = rating + increase
WHERE sid = sailor_sid
Sudarshan 12
Stored Procedures: Examples
Sudarshan 14
EXAMPLE FOR NESTED PROCEDURE
CREATE PROCEDURE UPDATE_SALARY
(IN employee_number VARCHAR(6), IN rating INTEGER)
LANGUAGE SQL
BEGIN
SET counter = 10;
WHILE (counter > 0) DO
IF (rating = 1)
THEN
UPDATE employee
SET salary = salary * 1.10, bonus = 1000
WHERE empno = employee_number;
ELSEIF (rating = 2)
THEN
UPDATE employee
SET salary = salary * 1.05, bonus = 500
WHERE empno = employee_number;
ELSE
UPDATE employee
SET salary = salary * 1.03, bonus = 0
WHERE empno = employee_number;
END IF;
SET counter = counter – 1;
END WHILE;
END Sudarshan 15
Example
LANGUAGE value of SQL and the BEGIN...END
block, which forms the procedure body, are
particular to an SQL procedure
1)The stored procedure name is
UPDATE_SALARY_1.
2)The two parameters have data types of
VARCHAR(6) and INTEGER. Both are input
parameters.
3)LANGUAGE SQL indicates that this is an SQL
procedure, so a procedure body follows the other
parameters.
4)The procedure body consists of a single SQL
UPDATE statement, which updates rows in the
employee table. Sudarshan 16
Some Valid SQL Procedure Body
Statements
• CASE statement
• FOR statement
• GOTO statement
• IF statement
• ITERATE statement
• RETURN statement
• WHILE statement
Sudarshan 17
Calling Stored Procedures
EXEC SQL BEGIN DECLARE SECTION
Int sid;
Int rating;
EXEC SQL END DECLARE SECTION