Summary: in this tutorial, you will learn about variables including declaring variables, setting their values, and assigning value fields of a record to variables. Show
What is a variableA variable is an object that holds a single value of a specific type e.g., integer, date, or varying character string. We typically use variables in the following cases:
Declaring a variableTo declare a variable, you use the
The By default, when a variable is declared, its value is set to Between the variable name and data type, you can use the optional
To declare multiple variables, you separate variables by commas:
Assigning a value to a variableTo assign a value to a variable, you use the
Using variables in a queryThe following
Now, you can put everything together and execute the following code block to get a list of products whose model year is 2018:
Note that to execute the code, you click the Execute button as shown in the following picture: The following picture shows the output: Storing query result in a variableThe following steps describe how to store the query result in a variable: First, declare a variable named
Second, use the
Third, output the content of the
Or you can use the
or
The output in the messages tab is as follows:
To hide the number of rows affected messages, you use the following statement:
Selecting a record into variablesThe following steps illustrate how to declare two variables, assign a record to them, and output the contents of the variables: First, declare variables that hold the product name and list price:
Second, assign the column names to the corresponding variables:
Third, output the content of the variables:
Accumulating values into a variableThe following stored procedure takes one parameter and returns a list of products as a string:
In this stored procedure:
The following statement executes the
The following picture shows the partial output: In this tutorial, you have learned about variables including declaring variables, setting their values, and assigning value fields of a record to the variables. Can stored procedure return value MySQL?To return a value from stored procedure, you need to use user defined session specific variable. Add @ symbol before variable name. Now second call for difference of two values. Call the stored procedure.
Can procedure return a value?A stored procedure does not have a return value but can optionally take input, output, or input-output parameters. A stored procedure can return output through any output or input-output parameter.
How declare variable in MySQL procedure?To declare a variable inside a stored procedure, you use the DECLARE statement as follows:. DECLARE variable_name datatype(size) [DEFAULT default_value]; ... . DECLARE totalSale DEC(10,2) DEFAULT 0.0; ... . DECLARE x, y INT DEFAULT 0; ... . SET variable_name = value; ... . DECLARE total INT DEFAULT 0; SET total = 10;. What is stored procedure result set?In addition to returning output parameters, a stored procedure can return a result set (that is, a result table associated with a cursor opened in the stored procedure) to the application that issues the CALL statement. The application can then issue fetch requests to read the rows of the result set cursor.
|