Professional Documents
Culture Documents
PROCEDURES
Introduction, Structure & Applications
Introduction
• Stored procedures perform the following functions:
• Enable multiple database operations to be conducted at the same
time.
#Procedure body
END $$
Example
• A procedure that logs all insertions of data into the
customer table:
• The procedure captures all the details needed for the customer
data insertion and the username that calls the procedure.
• Add two insert statements: one for the customer data, the
second for the logging data
Create the Logtable
• LogID (PK – Auto Increment),
• userName,
• activity,
• details,
• transactionTime
Consider the log table to have the following structure:
LOGTABLE(logID (PK), userName,activity,details,transactionTime)
DELIMITER $$
CREATE PROCEDURE customerEditor (customerID INT,customerName
CHAR(45), birthDate DATE,customerMobile CHAR(25), userNm CHAR(25))
BEGIN
INSERT INTO
customer(customerID,customerName,birthDatecustomerMobile)
VALUES (customerID,customerName,birthdate,customerMobile);
END $$
DELIMITER;
TO RUN the Stored Procedure (in the Resultset tab):
• These may include – invalid data types, primary and foreign key
violations etc.
• EXIT Handlers
• Handle errors by allowing an exit of the procedure without the database
reporting any errors by itself.
• CONTINUE Handlers:
• Enable the procedure to continue execution by side stepping the error
Continue Handlers: Example
• Consider handling the error of duplicate entry in the
member table by informing the user that the account
number used already exists.
• MEMBER
• (accountNo,
• memberName,
• phoneNo,
• currentBalance)