• Tidak ada hasil yang ditemukan

Assigning Values to Variables

Dalam dokumen Book MySQL Stored Procedure Programming (Halaman 78-83)

You manipulate variable values with theSETstatement, which has the following syn- tax:

SET variable_name = expression [,variable_name = expression ...]

As you can see, it is possible to perform multiple assignments with a single SET statement.

Most languages do not require aSETstatement for variable assignment, and conse- quently, one of the easiest mistakes to make when getting started is to try to assign a value to a variable without specifyingSET, as in Example 3-2.

As is often the case with stored program compilation errors, the error message does not directly identify the absence of the SETstatement, so when checking your pro- gram for strange compilation errors, double check that all variable assignments includeSET.

Parameters

Parameters are variables that can be passed into—or out of—the stored program from the calling program. Parameters are defined in the CREATE statement for the function or procedure as follows:

CREATE PROCEDURE|FUNCTION(

[[IN|OUT|INOUT] parameter_name data_type ...])

The parameter names follow the same naming rules that apply to variables. The data_typecan be any of the types available to local variables. Parameters can be asso- ciated with anIN, OUT, orINOUT attribute:

IN

Unless otherwise specified, parameters assume theINattribute. This means that their value must be specified by the calling program, and any modifications made to the parameter in the stored program cannot be accessed from the call- ing program.

OUT

An OUT parameter can be modified by the stored program, and the modified value can be retrieved from the calling program. The calling program must sup- ply a variable to receive the output of theOUTparameter, but the stored program itself has no access to whatever might be initially stored in that variable. When Example 3-2. Attempting to manipulate a variable without the SET statement

mysql> CREATE PROCEDURE no_set_stmt( ) BEGIN

DECLARE i INTEGER;

i=1;

END;

$$

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that

corresponds to your MySQL server version for the right syntax to use near 'procedure no_

set_stmt( ) BEGIN

DECLARE i INT;

i=1;

END' at line 1

the stored program commences, the value of anyOUTvariables appear as NULL, regardless of what value they may have been assigned in the calling program.

INOUT

AnINOUTparameter acts both as anINand as anOUTparameter. That is, the call- ing program may supply a value, the stored program itself may modify the value of the parameter, and the calling program may access this changed value when the stored program completes.

TheIN,OUT, andINOUT keywords apply only to stored procedures and not to stored functions. In stored functions all parameters behave asINparameters (although you cannot specify theIN keyword).

The next three examples illustrate these principles.

First, although MySQL lets us change the value of anINparameter in a stored pro- gram, the change cannot be seen by the calling program. The stored program in Example 3-3 prints and then modifies the value of the parameter. While modifica- tion of the input parameter is allowed within the stored program, the original vari- able (@p_in) is unchanged.

Example 3-3. Example of an IN parameter

mysql> CREATE PROCEDURE sp_demo_in_parameter(IN p_in INT) BEGIN

/* We can see the value of the IN parameter */

SELECT p_in;

/* We can modify it*/

SET p_in=2;

/* show that the modification took effect */

select p_in;

END;

/* This output shows that the changes made within the stored program cannot be accessed from the calling program (in this case, the mysql client):*/

mysql> SET @p_in=1

Query OK, 0 rows affected (0.00 sec) mysql> CALL sp_demo_in_parameter(@p_in)

+---+---+

| p_in | We can see the value of the IN parameter | +---+---+

| 1 | We can see the value of the IN parameter | +---+---+

1 row in set (0.00 sec)

+---+---+

| p_in | IN parameter value has been changed | +---+---+

Next, in Example 3-4 we examine the behavior of an OUTparameter. Although the calling program has initialized the OUTparameter with a value, the stored program does not see that value. The calling program, however, sees the changed values when the procedure completes execution.

| 2 | IN parameter value has been changed | +---+---+

1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> SELECT @p_in,'We can''t see the changed value from the calling program' +---+---+

| @p_in | We can't see the changed value from the calling program | +---+---+

| 1 | We can't see the changed value from the calling program | +---+---+

1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Example 3-4. Example of an OUT parameter

mysql> CREATE PROCEDURE sp_demo_out_parameter(OUT p_out INT) BEGIN

/* We can't see the value of the OUT parameter */

SELECT p_out,'We can''t see the value of the OUT parameter';

/* We can modify it*/

SET p_out=2;

SELECT p_out,'OUT parameter value has been changed';

END;

mysql> SET @p_out=1

Query OK, 0 rows affected (0.00 sec) mysql> CALL sp_demo_out_parameter(@p_out)

+---+---+

| p_out | We can't see the value of the OUT parameter in the stored program | +---+---+

| NULL | We can't see the value of the OUT parameter in the stored program | +---+---+

1 row in set (0.00 sec)

+---+---+

| p_out | OUT parameter value has been changed | +---+---+

| 2 | OUT parameter value has been changed | Example 3-3. Example of an IN parameter (continued)

Finally, Example 3-5 shows that the value of anINOUTparameter can be seen by the stored program, modified, and returned in its modified form to the calling program.

+---+---+

1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> SELECT @p_out,"Calling program can see the value of the changed OUT parameter"

+---+

| Calling program can see the value of the changed OUT parameter | +---+

| 2 | +---+

1 row in set (0.00 sec)

Example 3-5. Example of an INOUT parameter

mysql> CREATE PROCEDURE sp_demo_inout_parameter(INOUT p_inout INT) BEGIN

SELECT p_inout,'We can see the value of the INOUT parameter in the stored program';

SET p_inout=2;

SELECT p_inout,'INOUT parameter value has been changed';

END;

//

Query OK, 0 rows affected (0.00 sec) SET @p_inout=1

//

Query OK, 0 rows affected (0.00 sec) CALL sp_demo_inout_parameter(@p_inout) //

+---+---+

| p_inout | We can see the value of the INOUT parameter in the stored program | +---+---+

| 1 | We can see the value of the INOUT parameter in the stored program | +---+---+

1 row in set (0.00 sec)

+---+---+

| p_inout | INOUT parameter value has been changed | +---+---+

| 2 | INOUT parameter value has been changed | +---+---+

1 row in set (0.00 sec)

Example 3-4. Example of an OUT parameter (continued)

Dalam dokumen Book MySQL Stored Procedure Programming (Halaman 78-83)