Our next chapter, ‘‘Stored Procedures and Parameters,’’ will relate how you cansave multiple SQL statements in a procedure and make use of parameters in those procedures to add a degree
Trang 1Our next chapter, ‘‘Stored Procedures and Parameters,’’ will relate how you can
save multiple SQL statements in a procedure and make use of parameters in
those procedures to add a degree of generality to your SQL commands We’ll
also talk about the possibility of creating your own custom functions and explain
how functions differ from stored procedures Much like the views discussed in
Chapter 13, stored procedures and custom functions are useful objects that you
can create and store in your database to provide some extra polish and
func-tionality for your systems
Looking Ahead 161
Trang 2This page intentionally left blank
Trang 3chapter 16
Stored Procedures
and Parameters
Keywords Introduced:
Up until now, all of our data retrieval has been accomplished with a single statement Even the set logic seen in the previous chapter was accomplished by combining multipleSELECTs into a single statement We’re now going to dis-cuss a new scenario in which multiple statements can be saved into a single object
known as a stored procedure.
In broad terms, there are two general reasons why you might want to use stored procedures:
■ To save multiple SQL statements in a single procedure
■ To use parameters in conjunction with your SQL statements
Stored procedures can, in fact, consist of a single SQL statement and contain no parameters But the real value of stored procedures becomes evident when they contain multiple statements or parameters
The subject of stored procedures is quite complex In this brief review of the subject, we’ll focus on an overview of the second stated reason—that of using parameters in stored procedures This is something that relates directly to the issue of how to best retrieve data from a database As you’ll see, the ability to add
163
Trang 4parameters to a SELECT statement turns out to be a very useful feature in everyday use
The use of stored procedures to contain multiple statements is beyond the scope of this book Basically, the ability to store multiple statements in a procedure means that you can create complex logic and execute it all at once as a single transaction For example, you might have a business requirement to take
an incoming order from a customer and quickly evaluate it before accepting it from the customer This procedure might involve checking to make sure that the items are in stock, verifying that the customer haa a good credit rating, and getting an initial estimate as to when it can be shipped This situation would require multiple SQL statements with some added logic to determine what kind
of message to return if all were not well with the order All of that logic could be placed into a single stored procedure, which would enhance the modularity of the system With everything in one procedure, that logic could be executed from any calling program, and it would always return the same result
Creating Stored Procedures
Before we get into the details of how to utilize stored procedures, let’s cover the mechanics of how they are created and maintained The syntax varies sig-nificantly among different databases
The general format for creating a stored procedure in Microsoft SQL Server is:
CREATE PROCEDURE ProcedureName
AS
OptionalParameterDeclarations
BEGIN
SQLStatements
END
TheCREATE PROCEDURE keyword allows you to issue a single command that creates the procedure The procedure itself can contain any number of SQL statements and can also contain parameter declarations We’ll talk about the parameter declaration syntax later The SQL statements are listed between
BEGINandEND keywords
Chapter 16 ■ Stored Procedures and Parameters
164
Trang 5D A T A B A S E D I F F E R E N C E S : M y S Q L a n d O r a c l e
The general format for creating a stored procedure in MySQL is slightly more complex.
The format is:
DELIMITER $$
CREATE PROCEDURE ProcedureName ()
BEGIN
SQLStatements
END$$
DELIMITER ;
MySQL requires delimiters when executing multiple statements The normal delimiter is a
semi-colon The first line in the above code temporarily changes the delimiter from a semicolon to two
dollar signs Any needed parameters are specified between the parentheses on the CREATE
PROCEDURE line Then each SQL statement listed between the BEGIN and END keywords
must have a semicolon at the end of the statement The dollars signs are written after the END
keyword to denote that the CREATE PROCEDURE command is completed Finally, another
DELIMITER statement is placed at the end to change the delimiter back to a semicolon.
The procedure for creating stored procedures in Oracle is quite a bit more involved and is beyond the
scope of this book In order to create a stored procedure for a SELECT statement in Oracle, you will
need to first create an object called a package The package will contain two basic components: a
specification and a body The specification component specifies how to communicate with the body
component The body component contains the SQL statements, which are at the heart of the stored
procedure.
Here’s an example of how to create a stored procedure that can be used to
exe-cute this singleSELECTstatement:
SELECT *
FROM Customers
The procedure will be named ProcedureOne In Microsoft SQL Server, the
statement to create the procedure is:
CREATE PROCEDURE ProcedureOne
AS
BEGIN
SELECT *
FROM Customers
END
Creating Stored Procedures 165