EX: 5
Create cursor for Employee table & extract the values from the table. Declare the variables ,Open the cursor & extrct the values from the cursor. Close the cursor. Employee(E_id, E_name, Age, Salary)
1. Start the procedure
-
Give the procedure a name.
-
Begin the procedure block.
2. Declare variables
-
Create variables to store column values from the table.
-
All variables must be declared at the beginning.
3. Declare the cursor
-
Define a cursor that selects rows from the table.
-
This cursor will be used to read records one by one.
4. Declare a handler
-
Create a handler to detect the end of the cursor.
-
This handler will stop the loop when there are no more rows.
5. Open the cursor
-
Open the cursor to start fetching rows.
6. Loop and fetch data
-
Start a loop.
-
Fetch one row from the cursor into variables.
-
Check if the cursor reached the end.
-
If not end, process the row (e.g., display or store values).
7. Exit the loop
-
When no more rows exist, exit the loop.
8. Close the cursor
-
Close the cursor to release resources.
9. End the procedure
-
End the procedure block.
-
Procedure is ready to be called.
Step: Create DB, Use DB, Create Table.
CREATE TABLE Employee (
E_id INT PRIMARY KEY,
E_name VARCHAR(50),
Age INT,
Salary DECIMAL(10,2)
);
No comments:
Post a Comment