Lab 09 Stored Procedures
Lab 09 Stored Procedures
Stored procedures
A stored procedure is a segment of declarative SQL statements stored inside the database
catalog. A stored procedure can be invoked by triggers, other stored procedures, and
applications such as Java, Python, PHP, etc.
Database Lab 9
(Command line):
Example: The GetAllProducts() stored procedure selects all products from the products table.
The first command is DELIMITER // , which is not related to the stored procedure syntax.
The DELIMITER statement changes the standard delimiter which is semicolon ( ; ) to
another. In this case, the delimiter is changed from the semicolon( ; ) to double-
slashes // Why do we have to change the delimiter? Because we want to pass the stored
procedure to the server as a whole rather than letting mysql tool interpret each
statement at a time. Following the END keyword, we use the delimiter // to indicate the
end of the stored procedure. The last command ( DELIMITER; ) changes the delimiter
back to the semicolon (;).
We use the CREATE PROCEDURE statement to create a new stored procedure. We
specify the name of stored procedure after the CREATE PROCEDURE statement. In this
case, the name of the stored procedure is GetAllProducts . We put the parentheses after
the name of the stored procedure.
The section between BEGIN and END is called the body of the stored procedure. You put
the declarative SQL statements in the body to handle business logic. In this stored
procedure, we use a simple SELECT statement to query data from the products table.
Database Lab 9
(Workbench - GUI):
Declaring variables
To declare a variable inside a stored procedure, you use the DECLARE statement as follows:
DECLARE variable_name datatype(size) DEFAULT default_value;
First, you specify the variable name after the DECLARE keyword. The variable name must follow
the naming rules of MySQL table column names.
Second, you specify the data type of the variable and its size. A variable can have any MySQL data
types such as INT , VARCHAR , DATETIME , etc.
Third, when you declare a variable, its initial value is NULL. You can assign the variable a default
value using the DEFAULT keyword.
MySQL allows you to declare two or more variables that share the same data type using a single Declare
statement as following:
Assigning variables
Once you declared a variable, you can start using it. To assign a variable another value, you use
the SET statement, for example:
Besides the SET statement, you can use the SELECT INTO statement to assign the result of a query,
which returns a scalar value, to a variable. See the following example:
FROM products
A variable has its own scope that defines its lifetime. If you declare a variable inside a stored
procedure, it will be out of scope when the END statement of stored procedure reached.
If you declare a variable inside BEGIN END block, it will be out of scope if the END is reached.
You can declare two or more variables with the same name in different scopes because a
variable is only effective in its own scope. However, declaring variables with the same name in
different scopes is not good programming practice.
A variable that begins with the @ sign is session variable. It is available and accessible until the
session ends.
Database Lab 9
Prerequisites
DROP TABLE IF EXISTS emp;
Verify Using:
select * from emp;
BEGIN
END
DELIMITER ;
BEGIN
END
DELIMITER ;
CALL sample_sp_with_params(1,@out,@inout);
select @out,@inout;
BEGIN
END
DELIMITER ;
You can verify the values of OUT and INOUT parameters as:
select @out,@inout;
Database Lab 9
RETURNS INT
BEGIN
RETURN count;
END
DELIMITER ;
RETURNS VARCHAR(20)
BEGIN
RETURN oldName;
END
DELIMITER ;
Drop Commands