Using OUT Parameters
As I mentioned earlier in the chapter, stored procedures can have IN parameters (as you've seen) as well as OUT and INOUT parameters. IN parameters work just like those in stored functions. OUT parameters allow you to assign a value to a variable that exists outside of the stored procedure. INOUT can serve both purposes. MySQL assumes that all parameters are IN unless you say otherwise, by indicating such before the argument's name:
CREATE PROCEDURE myproc (OUT x INT, INOUT y CHAR)...
When calling these procedures, you'll normally involve user-defined variables in the mysql client as the OUT argument (Figure 11.18):
CREATE PROCEDURE assign (OUT n INT)
SET n = 10;
CALL assign (@num);
Figure 11.18. This trivial examples demonstrates how OUT arguments can assign values to user variables.
The CALL line invokes the procedure, providing the user variable @num as its argument. In the procedure, this variable is given a value of 10. To see the variable's value, it must then be selected.
As an example of this concept, the next stored procedure will return the client ID if a user successfully enters the proper login name and password. This procedure also makes use of the SELECT...INTO query, which is introduced in the sidebar.
In this series of steps I use a special kind of SELECT query, referred to as SELECT...INTO. Such a query selects values and immediately assigns those values to variables. The general syntax is
SELECT col1, col2 INTO var1, var2 FROM tablename
You can have as many variables as you want, as long as they match the number of columns selected. You can also add WHERE and other clauses as needed.
To use OUT parameters:
Log in to the mysql client as a user permitted to create stored functions in the accounting database, if you have not already.
Select the accounting
database, if it is not already selected.
Begin defining the stored procedure.
CREATE PROCEDURE check_login
(name VARCHAR(20), pass VARCHAR(20), OUT cid INT)
This procedure takes three arguments. The first two are of type IN
and are both strings. The third is an OUT
parameter of type integer.
Retrieve the client ID.
SELECT client_id INTO cid FROM logins
WHERE login_name=AES_ENCRYPT(name, 'w1cKet')
This is a simple login query that returns the client ID associated with the password and username, assuming that the correct values were entered. The selected value, assuming there is one, will be immediately assigned to cid
Once again, the routine's argumentsname
, specificallyare not put within quotation marks even though they are strings.
If you need to fetch and handle a lot of data in a stored procedure (more than the single value returned by check_login), you'll need to make use of cursors. I don't discuss this topic in detail in the book, because:
They aren't often necessary.
Some people suggest that their use implies bad programming form (i.e., there's probably a better way to do what you're doing).
Still, if you're curious or think you need cursors, check out the MySQL manual. You'll also probably need to learn about declaring handlers, as cursors and handlers are often used together.
Complete the procedure and change the delimiter back to the semicolon (Figure 11.19
Figure 11.19. This procedure runs a SELECT...INTO query, assigning the returned value to an OUT argument.
Test the procedure using both valid and invalid access information (Figure 11.20
CALL check_login ('username', 'password', @id);
CALL check_login ('bad', 'bad', @id);
Figure 11.20. The user variable @id is used when calling the stored procedure. In the procedure, @id is assigned the client's ID, if the proper username and password combination is submitted.