IN parameter

Let's begin by dropping the procedure that we created in the previous section with the help of the following code: 

USE lahmansbaseballdb;
DROP PROCEDURE IF EXISTS getplayergameinfo;

In the preceding code, there is an IF EXISTS in the DROP PROCEDURE statement. This will drop the procedure if it exists, but won't throw an error after dropping it if it doesn't exist.

Next, you will create the procedure, but this time it will have a couple of parameters so that you can change the WHERE clause values: 

USE lahmansbaseballdb;
DELIMITER $$
CREATE PROCEDURE getplayergameinfo
(
IN yearid_in year,
IN hits_in smallint
)
BEGIN
SELECT p.playerid, birthyear, a.yearid,
a.teamid,G_defense AS defensegames,
H AS numberofhits
FROM appearances AS a
JOIN people AS p ON p.playerid = a.playerid
JOIN batting AS b ON a.playerid = b.playerid
AND a.yearid = b.yearid
AND a.teamid = b.teamid
WHERE b.yearid = yearid_in AND h > hits_in
ORDER BY p.playerid, a.yearid,
a.teamid, G_defense, H;
END $$
DELIMITER ;

By doing this, you will have two parameters inside the parentheses after the procedure name: yearid_in and hits_in. They are declared with IN at the beginning, the name of the parameter, the data type of the parameter, and separated by a comma.

Then, you can call the procedure with the parameters shown in the following code: 

USE lahmansbaseballdb; 
CALL getplayergameinfo(2016, 0);

The preceding code will return the results shown in the following screenshot:

The call to the stored procedure allows you to put in any valid year for yearid_in and any smallint for hits_in. This will put those values into the WHERE clause and return the results based on those values.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.33.157