DECLARE creates a variable or procedural object. For a stored procedure, it can name the variable and specify its data type, giving later SQL a value to set, check, or reuse. section 22.1.1
Declare a title ID
Inside a stored procedure, declare an integer variable and give it a starting value: DECLARE _TitleID int DEFAULT 101; You can instead assign a value later with SET, including a value returned by a subquery. DECLARE therefore defines the variable, while SET supplies or changes its value. section 22.1.1
DECLARE also creates a cursor. Its declaration contains the SELECT query that defines the rows the cursor will use. The cursor must then be OPENed, FETCHed one row at a time, and CLOSEd in that order. section 22.2.1
Keep these practical distinctions in mind:
- MySQL can use another variable-declaration method outside stored procedures, but inside a stored procedure it uses DECLARE with a data type. section 22.1.1 section 13.1.1
- Use SELECT to inspect a variable’s current value while checking a script or troubleshooting an unexpected result. section 13.1.1
- Reusable variables let one query select different titles by changing one value, such as The Sum Also Rises or Pride and Predicates. section 13.1.3
DECLARE _TitleID int DEFAULT 101;
A compact example of declaring an integer variable with a default value. section 22.1.1
