The Language of SQL - Chapter 16

advertisement
The Language of SQL
Chapter 16
Stored Procedures and Parameters
This chapter introduces stored procedures, which allow you to define a set of SQL to run by
calling the procedure. The parameters mentioned in the opening section are option input
parameters to your stored procedure. With these input parameters you can allow the caller of the
stored procedure to specify data to run the SQL set against, say a customer name or number to
gather data on. Stored procedures are particularly useful when interacting with applications or
users in a complex data set, or a data set that may change. You can define certain procedures
that may be run on data within the database. If the data structure is changed or augmented you
can simply update the procedure to handle the new configuration and allow the user to call the
same procedure with the same input parameters.
Creating Stored Procedures
You can create a stored procedure by using the syntax below.
Syntax
MSSQL/Oracle
CREATE PROCEDURE ProcedureName
AS
OptionalParameterDeclarations
BEGIN
SQLStatements
END
MySQL
DELIMITER $$
CREATE PROCEDURE ProcedureName ()
BEGIN
SQLStatements
END$$
DELIMITER ;
As you can see the MySQL syntax is slightly different than the MSSQL and Oracle Syntax, you
must change the delimiter to something different in MySQL ($$ in the example) or MySQL will see
your semicolons (;) in the SQLStatements as ending the stored procedure; which will likely cause
an syntax error.
The ProcedureName defines what the user will call in order to run your stored procedure. The
OptionalParameterDeclarions cover your optional input parameters. Finally the SQLStatements
area is where you put all your SQL statements that will be executed as part of this stored
procedure. These can be any SQL statements you can run on your own, such as SELECT,
UPDATE, INSERT and DELETE statements.
Example
DELIMITER $$
CREATE PROCEDURE ProcedureOne ()
BEGIN
SELECT *
FROM Customers;
END$$
DELIMITER ;
Since our Raspberry Pi uses MySQL, I chose to use the MySQL syntax in the example. This
stored procedure, named ProcedureOne, simply grabs all rows from the Customers table.
RPi
mysql> DELIMITER $$
mysql> CREATE PROCEDURE ProcedureOne ()
-> BEGIN
-> SELECT *
-> FROM Customers;
-> END$$
Query OK, 0 rows affected (0.30 sec)
mysql> DELIMITER ;
Parameters in Stored Procedures
As stated in the opening of the notes, parameters allow you to give the user a little control of the
stored procedure. You can allow a stored procedure to affect certain customers in a table, etc.
Taking the stored procedure from the previous section, we can allow the user to specify a specific
CustomerID to retrieve.
Example
DELIMITER $$
CREATE PROCEDURE CustomerProcedure
(CustID INT)
BEGIN
SELECT *
FROM Customers
WHERE CustomerID = CustID;
END$$
DELIMITER ;
As you can see, inside the parenthesis after the ProcedureName we defined a parameter named
CustID with a data type of INT (Integer, which should look familiar from creating our tables). We
then use that parameter in our SELECT statement in the WHERE clause (WHERE CustomerID =
CustID). When the user calls this stored procedure they will pass a CustID and the data returned
will pertain to that specific CustomerID.
RPi
mysql> DELIMITER $$
mysql> CREATE PROCEDURE CustomerProcedure
-> (CustID INT)
-> BEGIN
-> SELECT *
-> FROM Customers
-> WHERE CustomerID = CustID;
-> END$$
Query OK, 0 rows affected (0.01 sec)
mysql> DELIMITER ;
Executing Stored Procedures
Now that we can create stored procedures, it would be helpful to know how to use them. To do
this you, or the user, will use the CALL or EXEC statement.
Syntax
MSSQL
EXEC ProcedureName
@ParameterName1 = ParameterValue1
@ParameterName2 = ParameterValue2
…
@ParameterNameN = ParameterValueN
Oracle
EXEC ProcedureName(ParamerterValue1, ParameterValue2, …,
ParameterValueN)
MySQL
CALL ProcedureName (ParamerterValue1, ParameterValue2, …,
ParameterValueN)
Take note of the differences between database types, Oracle and MySQL are pretty close other
than the statement name (CALL vs EXEC), MSSQL is slightly different.
So, if we want to cal our first stored procedure, which has no input parameters defined, we can
omit any parameters in our call statement.
Example
CALL ProcedureOne;
RPi
mysql> CALL ProcedureOne;
+------------+-----------+----------+
| CustomerID | FirstName | LastName |
+------------+-----------+----------+
|
1 | William
| Smith
|
|
2 | Natalie
| Lopez
|
|
3 | Brenda
| Harper
|
|
4 |
|
|
|
5 | John
| Doe
|
|
6 | John
| Smith
|
|
7 | Brian
| Smith
|
|
8 | Jon
| Voight
|
+------------+-----------+----------+
8 rows in set (0.30 sec)
Query OK, 0 rows affected (0.30 sec)
As you can see here, we just called ProcedureOne with no parameters and we received the
results of doing the SELECT * FROM Customers statement defined in that procedure. Pretty
simple, now let's move on to our procedure that has an input parameter.
Example
CALL CustomerProcedure (2);
RPi
mysql> CALL CustomerProcedure (2);
+------------+-----------+----------+
| CustomerID | FirstName | LastName |
+------------+-----------+----------+
|
2 | Natalie
| Lopez
|
+------------+-----------+----------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
As you can see here we only received the results for CustomerID 2 since we passed 2 as our
input parameter.
Modifying and Deleting Stored Procedures
You can modify a stored procedure using the ALTER PROCEDURE statement. However, in
MySQL the functionality of the ALTER PROCEDURE statement is limited and the book
recommends to just delete and then create the procedure again rather than attempting to alter it.
Syntax
ALTER PROCEDURE ProcedureName
OptionalParameterDeclarations
AS
SQLStatements
END
As you can see the syntax is identical to the CREATE PROCEDURE syntax, other than saying
ALTER rather than CREATE. I didn’t specify MySQL here since it is not recommended in the
book.
Deleting a stored procedure is pretty simple as well using the DROP PROCEDURE statement.
Syntax
DROP PROCEDURE ProcedureName
The syntax is pretty simple, DROP PROCEDURE followed by the ProcedureName you want to
remove. Remember that this procedure is no longer available once dropped so users and
applications that may call this procedure will be impacted until it is created again.
Functions Revisited
This section only describes the differences between a stored procedure and a function. Basically
a function must only return a single value and a function can be called inline through other SQL
statements, like a SELECT statement.
Download