DECLARE
Syntax
1138
statement_list
END WHILE [
end_label
]
Labels are permitted for
BEGIN ... END
blocks and for the
LOOP
,
REPEAT
, and
WHILE
statements.
Label use for those statements follows these rules:
•
begin_label
must be followed by a colon.
•
begin_label
can be given without
end_label
. If
end_label
is present, it must be the same as
begin_label
.
•
end_label
cannot be given without
begin_label
.
• Labels at the same nesting level must be distinct.
• Labels can be up to 16 characters long.
To refer to a label within the labeled construct, use an
ITERATE
or
LEAVE
statement. The following
example uses those statements to continue iterating or terminate the loop:
CREATE PROCEDURE doiterate(p1 INT)
BEGIN
label1: LOOP
SET p1 = p1 + 1;
IF p1 < 10 THEN ITERATE label1; END IF;
LEAVE label1;
END LOOP label1;
END;
The scope of a block label does not include the code for handlers declared within the block. For details,
see
Section 13.6.7.2, “
DECLARE ... HANDLER
Syntax”
.
13.6.3.
DECLARE
Syntax
The
DECLARE
statement is used to define various items local to a program:
• Local variables. See
Section 13.6.4, “Variables in Stored Programs”
.
• Conditions and handlers. See
Section 13.6.7, “Condition Handling”
.
• Cursors. See
Section 13.6.6, “Cursors”
.
DECLARE
is permitted only inside a
BEGIN ... END
compound statement and must be at its start,
before any other statements.
Declarations must follow a certain order. Cursor declarations must appear before handler declarations.
Variable and condition declarations must appear before cursor or handler declarations.
13.6.4. Variables in Stored Programs
System variables and user-defined variables can be used in stored programs, just as they can be
used outside stored-program context. In addition, stored programs can use
DECLARE
to define local
variables, and stored routines (procedures and functions) can be declared to take parameters that
communicate values between the routine and its caller.
• To declare local variables, use the
DECLARE
statement, as described in
Section 13.6.4.1, “Local
Variable
DECLARE
Syntax”
.
• Variables can be set directly with the
SET
statement. See
Section 13.7.4, “
SET
Syntax”
.
• Results from queries can be retrieved into local variables using
SELECT ... INTO var_list
or
by opening a cursor and using
FETCH ... INTO var_list
. See
Section 13.2.8.1, “
SELECT ...
INTO
Syntax”
, and
Section 13.6.6, “Cursors”
.
Summary of Contents for 5.0
Page 1: ...MySQL 5 0 Reference Manual ...
Page 18: ...xviii ...
Page 60: ...40 ...
Page 396: ...376 ...
Page 578: ...558 ...
Page 636: ...616 ...
Page 844: ...824 ...
Page 1234: ...1214 ...
Page 1427: ...MySQL Proxy Scripting 1407 ...
Page 1734: ...1714 ...
Page 1752: ...1732 ...
Page 1783: ...Configuring Connector ODBC 1763 ...
Page 1793: ...Connector ODBC Examples 1773 ...
Page 1839: ...Connector Net Installation 1819 2 You must choose the type of installation to perform ...
Page 2850: ...2830 ...
Page 2854: ...2834 ...
Page 2928: ...2908 ...
Page 3000: ...2980 ...
Page 3122: ...3102 ...
Page 3126: ...3106 ...
Page 3174: ...3154 ...
Page 3232: ...3212 ...