11 Programming The Database
11 Programming The Database
1. Stored Procedures
2. Functions
3. Cursors
4. Triggers
5. Dynamic SQL
• Stored procedures are declared using the following syntax (I refer to MySQL in this presentation):
Create Procedure <proc-name>
(param_spec1, param_spec2, …, param_specn )
begin
-- execution code
end;
1. Change the delimiter (i.e., terminating character) of SQL statement from semicolon (;) to something else (e.g., //)
3. The body of the procedure is an SQL command to update the totalsalary column of the deptsal table.
4. Terminate the procedure definition using the delimiter you had defined in step 1 (//)
https://dev.mysql.com/doc/refman/8.0/en/
DECLARE radius
Rad_cursor
Pi constant NUMBER(8,7) := 3.1415926;
3
area NUMBER(14,2);
f
e
cursor rad_cursor is select * from RAD_VALS;
6
rad_val rad_cursor%ROWTYPE;
t
BEGIN c 8
open rad_cursor; h
fetch rad_cursor into rad_val; Rad_val
area:=pi*power(rad_val.radius,2);
insert into AREAS values (rad_val.radius, area);
AREAS
close rad_cursor; Radius Area
END; 3 28.27
• Some vendor DBMS permit native extensions to SQL for specifying the triggers
• e.g. PL/SQL in Oracle, Transact SQL in MS SQL Server
• Some DBMS also allow use of general purpose programming language instead of
SQL
• e.g. Java in Oracle, C#/VB in SQL Server