Databases Reference
In-Depth Information
Creating a Stored Procedure
We'll create a procedure that has the effect to change the page count for a specific
book, adding a specific number of pages. The book's ISBN, and number of pages to
add, will be the input parameters to this procedure.
Changing the Delimiter
The standard SQL delimiter is the semicolon, and this character will be used
inside our procedure to delimit SQL statements. However, the CREATE PROCEDURE
statement is itself a SQL statement, so we must come up with a way to indicate to the
MySQL parser where this statement ends. The query box has a Delimiter input box
that contains a semicolon by default, therefore, we change it to another string, which
by convention will be a double slash // .
Entering the Procedure
Then, we enter the procedure's code in the main query box:
CREATE PROCEDURE `add_page`(IN param_isbn VARCHAR(25), IN param_pages
INT, OUT param_message VARCHAR(100))
BEGIN
IF param_pages > 100 THEN
SET param_message = 'the number of pages is too big';
ELSE
UPDATE book SET page_count = page_count + param_pages WHERE
isbn=param_isbn;
SET param_message = 'success';
END IF;
END
//
After clicking Go , if the syntax is correct, we get a success message; if not, well it's
time to revise our typing abilities or to debug our syntax; unfortunately, MySQL
does not come with a procedure debugger.
 
Search WWH ::




Custom Search