Parameters in Stored Procedures All of the SELECT statements you have seen up until now have had a certain static quality due to the fact that they were written to retrieve data in one s
Trang 1D A T A B A S E D I F F E R E N C E S : M y S Q L
In MySQL, the previous example would look like:
DELIMITER $$
CREATE PROCEDURE ProcedureOne ()
BEGIN
SELECT *
FROM Customers;
END$$
DELIMITER ;
Remember that creating a stored procedure does not execute anything It simply creates the procedure so that it can be executed later Along with tables and views, the procedure will be visible in your database management tool so that you can view its contents
Parameters in Stored Procedures
All of the SELECT statements you have seen up until now have had a certain static quality due to the fact that they were written to retrieve data in one specific way The ability to add parameters to SELECT statements gives you the possi-bility of much greater flexipossi-bility
The term parameter in SQL statements is similar to the term variable, as it is used
in other computer languages A parameter is basically a value that is passed to a SQL statement by the calling program It can have whatever value the user spe-cifies at the time the call is made
Let’s start with a simple example We have a SELECT statement that retrieves data from a Customers table Rather than select all customers, we would like the
SELECTto retrieve data for only one specific CustomerID number However, we don’t want to code the number directly in the SELECT statement We want the SELECT statement to be general enough so it can accept any provided CustomerID number and then execute with that value TheSELECT statement without any parameters is simply:
SELECT *
FROM Customers
Chapter 16 ■ Stored Procedures and Parameters
166
Trang 2Our goal is to add aWHEREclause that will allow us to select data for a particular
customer In a general form, we’d like theSELECTstatement to be:
SELECT *
FROM Customers
WHERE CustomerID¼ ParameterValue
In Microsoft SQL Server, the creation of such a stored procedure can be
accomplished with:
CREATE PROCEDURE CustomerProcedure
(@CustID INT )
AS
BEGIN
SELECT *
FROM Customers
WHERE CustomerID ¼ @CustID
END
Notice the addition of the second line, which specifies the CustID parameter in
the procedure In Microsoft SQL Server, the @ symbol is used to denote a
para-meter The INT keyword is placed after the parameter to indicate that this
parameter will have an integer value The same parameter name is used in the
WHEREclause
D A T A B A S E D I F F E R E N C E S : M y S Q L
In MySQL, the command to create an equivalent stored procedure is:
DELIMITER $$
CREATE PROCEDURE CustomerProcedure
(CustID INT )
BEGIN
SELECT *
FROM Customers
WHERE CustomerID ¼ CustID;
END$$
DELIMITER ;
Notice that MySQL doesn’t require the @ symbol to denote a parameter.
Trang 3When a stored procedure is executed, the calling program passes a value for the parameter, and the SQL statement is executed as if that value were part of the statement
It should also be noted that the parameters discussed previously are input para-meters As such, they contain values that passed into the stored procedure Stored procedures can also include output parameters, which can contain values passed back to the calling program It’s beyond the scope of this book to discuss the various nuances of how to specify both input and output parameters in stored procedures
Executing Stored Procedures
After stored procedures are created, how are they executed? The syntax varies between databases Microsoft SQL Server provides the EXEC keyword to run stored procedures
In Microsoft SQL Server, the following statement will execute the ProcedureOne procedure:
EXEC ProcedureOne
When this statement is executed, it brings back the results of theSELECT state-ment contained in the stored procedure
The ProcedureOne procedure didn’t have any parameters, so the syntax is simple How do you execute procedures with input parameters? The following executes the CustomerProcedure procedure for a CustID value of 2:
EXEC CustomerProcedure
@CustID ¼ 2
D A T A B A S E D I F F E R E N C E S : M y S Q L
Rather than using EXEC, MySQL uses a CALL keyword to execute stored procedures, and the syntax for stored procedures with parameters is slightly different The equivalent of the previous two EXEC statements in MySQL is:
CALL ProcedureOne;
CALL CustomerProcedure (2);
Chapter 16 ■ Stored Procedures and Parameters
168
Trang 4Modifying and Deleting Stored Procedures
Once a stored procedure is created, it can be modified Just as theALTER VIEW
keyword was used to modify views, theALTER PROCEDUREkeyword is used to
modify stored procedures The syntax is identical to theCREATE PROCEDURE,
except that the word ALTER is used in place of CREATE Just as the CREATE
PROCEDUREhas a slightly different syntax for each database, so does theALTER
You’ve already seen this example of creating a stored procedure with Microsoft
SQL Server:
CREATE PROCEDURE CustomerProcedure
(@CustID INT )
AS
BEGIN
SELECT *
FROM Customers
WHERE CustomerID ¼ @CustID
END
After this procedure is created, if you wanted to alter the procedure to select only
the top five rows from the Customers table, the command to accomplish that
would be:
ALTER PROCEDURE CustomerProcedure
(@CustID INT )
AS
BEGIN
SELECT
TOP 5 *
FROM Customers
WHERE CustomerID ¼ @CustID
END
D A T A B A S E D I F F E R E N C E S : M y S Q L
MySQL provides an ALTER PROCEDURE command, but it has limited functionality To alter the
content of a stored procedure in MySQL, you need to issue a DROP PROCEDURE and then a
CREATE PROCEDURE with the new content.
Deleting a stored procedure is even simpler Just as the DROP VIEW deletes a
view, theDROP PROCEDUREstatement deletes a procedure
Trang 5Here’s how the stored procedure named CustomerProcedure can be deleted:
DROP PROCEDURE CustomerProcedure
Functions Revisited
In Chapter 4, we talked about the built-in scalar functions that are available in SQL For example, we used character functions such asLEFTand mathematical functions such asROUND In Chapter 10, we discussed aggregate functions such
asMAX
In addition to the built-in functions in SQL, developers can create their own functions and save them in a database The procedure for creating functions is very similar to the procedure for creating stored procedures SQL provides the keywords CREATE FUNCTION, ALTER FUNCTION, and DROP FUNCTION, which work very much like CREATE PROCEDURE, ALTER PROCEDURE, and
Due to the advanced nature of this topic, we’re not going to provide specific examples of this functionality However, we’ll spend a few moments explaining the differences between using stored procedures and functions
Both stored procedures and functions can be saved in a database These entities are saved as separate objects in a database, much like tables or views The pro-cedures for saving and modifying stored propro-cedures and functions are very similar The sameCREATE,ALTER, andDROPcommands for stored procedures can be used for functions
The difference between the two lies in how they are used and in their capabilities There are two main distinctions between stored procedures and functions:
even have zero output parameters In contrast, a function must always contain exactly one output parameter In other words, when you call a function, you always get back a single value
procedure cannot be directly referenced in aSELECTstatement In
contrast, functions can be referenced within statements, as seen in
Chapters 4 and 10 After you define your own function, you’ll reference that function by the name you specified when the function was created
Chapter 16 ■ Stored Procedures and Parameters
170