DBMS Exp9
DBMS Exp9
DBMS Exp9
Experiment No-9
Create a procedure:
Pick a Delimiter :The delimiter is the character or string of characters which is used to
complete an SQL statement. By default we use semicolon (;) as a delimiter. But this causes
problem in stored procedure because a procedure can have many statements, and everyone
must end with a semicolon. So for your delimiter, pick a string which is rarely occur within
statement or within procedure. Here we have used double dollar sign i.e. $$.You can use
whatever you want. To resume using ";" as a delimiter later, say "DELIMITER ; $$". See
here how to change the delimiter :
mysql> DELIMITER $$ ;
Now the default DELIMITER is "$$". Let execute a simple SQL command :
Here we have created a simple procedure called library_data(), when we will execute the
procedure it will display all the data from "library" tables.
Call a procedure
The CALL statement is used to invoke a procedure that is stored in a DATABASE. Here is
the syntax:
-> //
MySQL Procedure : Parameter IN example
In the following procedure, we have used a IN parameter no (type integer) which accept a
number from the user. Within the body of the procedure, there is a SELECT statement which
rows from library table which has no of books greater than the parameter given in the
procedure :
-> BEGIN
-> END
-> //
Call a procedure
The CALL statement is used to invoke a procedure that is stored in a DATABASE. Here is
the syntax:
-> //
MySQL Procedure : Parameter OUT example
The following example shows a simple stored procedure that uses an OUT parameter. Within
the procedure MySQL MAX() function retrieves maximum no of books from library table for
a particular book.
-> BEGIN
-> END
-> //
In the body of the procedure, the parameter will get the maximum number of books from the
library table and return it to the max_books with return type as int and while calling the
procedure it the result is stored in @B variable which will further displayed.
The following example shows a simple stored procedure that uses an INOUT parameter and
an IN parameter. In this procedure it will the count of staff having salary greater than the
salary pass in the parameter as salary and it will return total count as total variable which.
While calling the procedure it will pass the salary and it will store the result in the @T
variable
-> BEGIN
-> END
-> //
Now, while calling the procedure 1 parameter is passed salary and result is stored in the
variable @T
Example:
-> BEGIN
-> END
-> //
MySQL: Cursors
A database cursor is a control structure that enables traversal over the records in a database.
Cursors are used by database programmers to process individual rows returned by database
system queries. Cursors enable manipulation of whole result sets at once. In this scenario, a
cursor enables the rows in a result set to be processed sequentially. In SQL procedures, a
cursor makes it possible to define a result set (a set of data rows) and perform complex logic
on a row by row basis. By using the same mechanics, an SQL procedure can also define a
result set and return it directly to the caller of the SQL procedure or to a client application.
MySQL supports cursors inside stored programs. The syntax is as in embedded SQL. Cursors
have these properties :
- Asensitive: The server may or may not make a copy of its result table
- Read only: Not updatable
- Nonscrollable: Can be traversed only in one direction and cannot skip rows
To use cursors in MySQL procedures, you need to do the following :
- Declare a cursor.
- Open a cursor.
- Fetch the data into variables.
- Close the cursor when done.
Declare a cursor:
The following statement declares a cursor and associates it with a SELECT statement that
retrieves the rows to be traversed by the cursor.
DECLARE cursor_name
CURSOR FOR select_statement
Open a cursor:
OPEN cursor_name
Fetch the data into variables :
This statement fetches the next row for the SELECT statement associated with the specified
cursor (which must be open) and advances the cursor pointer. If a row exists, the fetched
columns are stored in the named variables. The number of columns retrieved by the SELECT
statement must match the number of output variables specified in the FETCH statement.
This statement closes a previously opened cursor. An error occurs if the cursor is not open.
CLOSE cursor_name