Tutorial Addendum on SQL - MySQL - Stored Action Accent
| |
Cursors
A cursor is a data blazon that represents a affiliation handle to the achievement table of a baddest statement.
A cursor is actual agnate to a book handle in some additional programming languages.
There are several statements accompanying to the use of a cursor:
DECLARE account - To acknowledge a cursor capricious and accessory it to a baddest statement:
DECLARE cursor_variable CURSOR FOR select_statement;
OPEN account - To assassinate the associated baddest account and set the cursor to point to
the first row of the achievement table:
OPEN cursor_variable;
FETCH account - To back data from the accepted row into accepting variables, and set the
cursor to point to the next row of the achievement table:
FETCH cursor_variable INTO variable, variable, ...;
CLOSE account - To abutting the cursor:
CLOSE cursor_variable;
When a cursor alcove the end of the achievement table, the Back account will aftereffect in
an beheading error: 02000. But this absurdity can be adapted into a analytic action by
a "continue handler":
DECLARE EndOfData INTEGER;
DECLARE Abide Abettor FOR SQLSTATE 02000 SET EndOfData = 1;
Here is sample cipher that uses a cursor to bend through anniversary row of a table and calculate
the minimum amount and best value:
-- ProcedureCursor.sql
-- Absorb (c) 2004 by Dr. Yang
--
DROP2 DATABASE IF EXISTS HyTest;
CREATE DATABASE HyTest;
USE HyTest;
--
DROP Action IF EXISTS InitTable;
DELIMITER / ;
CREATE Action InitTable(IN N INTEGER)
BEGIN
Acknowledge I INTEGER;
SET I = 0;
WHILE I < N DO
Admit INTO MyTable Ethics (I, RAND()*N);
SET I = I + 1;
END WHILE;
END/
DELIMITER ; /
--
DROP Action IF EXISTS CheckTable;
DELIMITER / ;
CREATE Action CheckTable(OUT Admeasurement INTEGER,
OUT Min INTEGER, OUT Max INTEGER)
BEGIN
Acknowledge K, V INTEGER;
Acknowledge HasData INTEGER;
Acknowledge Aftereffect CURSOR FOR Baddest ID, Amount FROM MyTable;
Acknowledge Abide Abettor FOR SQLSTATE 02000 SET HasData = 0;
Accessible Result;
SET Admeasurement = 0;
SET Min = 999999;
SET Max = -999999;
SET HasData = 1;
Back Aftereffect INTO K, V;
WHILE HasData = 1 DO
SET Admeasurement = Admeasurement + 1;
IF V < Min THEN
SET Min = V;
END IF;
IF V > Max THEN
SET Max = V;
END IF;
Back Aftereffect INTO K, V;
END WHILE;
Abutting Result;
END/
DELIMITER ; /
--
DROP TABLE IF EXISTS MyTable;
CREATE TABLE MyTable (ID INTEGER, Amount INTEGER);
CALL InitTable(20);
CALL CheckTable(@Size, @Min, @Max);
--
SELECT Table summary: AS --- ;
SELECT @Size AS Admeasurement , @Min AS Minimum , @Max AS Best ;
SELECT Table detail : AS --- ;
SELECT * FROM MyTable Area ID < 20;
Output:
---
Table summary:
Size Minimum Maximum
20 6 19
---
Table detail :
ID Value
0 11
1 6
2 15
3 19
4 10
5 12
6 10
7 16
8 8
9 14
10 7
11 12
12 18
13 14
14 15
15 13
16 19
17 19
18 15
19 18
|
cursor, table, declare, integer, statement, select, variable, procedure, result, fetch, mytable, output, create, exists, value, hasdata, delimiter, checktable, handler, minimum, hytest, inittable, , output table, cursor variable, select statement, integer declare, integer begin declare, fetch result into, delimiter create procedure, declare continue handler, mysql stored procedure, stored procedure language, sql mysql stored, |
Also see ...
Select StatementsA baddest account is aswell alleged a concern statement. It is commonly acclimated to retrieve rows of data called from defined tables. The all encompassing syntax
Join TablesA accompany table is the achievement table of a accompany operation on two tables. There are several types of joinoperations:1. Cantankerous Accomp
JointTable.sql Archetype of Accompany TablesTo validate the accompany table logics mentioned in the antecedent section, I wrote thefollowing SQL code, JointTable.sql:
GROUP BY Clause"GROUP BY clause" modifies the abject table by alignment aboriginal rows into accumulation rows based on identical accumulated ethics of the defined accumulation columns. br
This affiliate describes:What is a transaction?How does MySQL abutment of transaction management?What are transaction abreast levels.How does M
Here is a simple analysis cipher on the absence transaction: Rollback.sql Absorb (c) 2004 by Dr. Yang SET AUTOCOMMIT = 0;USE test;DROP TABLE IF EXISTS User;CREATE TAB
Transaction Abreast LevelsAs we can see from antecedent sections, the appulse of a transaction in the accepted session is simple. However, circumstantial affairs in assorted sessions may ap
MySQL Transaction Abreast Akin Analysis Apprehend CommittedMy next analysis is about "read committed". Again, I started "session 1" in the first command window: