0% found this document useful (0 votes)
123 views

MySQL SELECT INTO Temporary Variable

ryui fgjm eryi yuii yuio ertui ghhk fbnm rtui tuio fghj fhjl ghi thi

Uploaded by

nagat4r
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
123 views

MySQL SELECT INTO Temporary Variable

ryui fgjm eryi yuii yuio ertui ghhk fbnm rtui tuio fghj fhjl ghi thi

Uploaded by

nagat4r
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

MySQL SELECT INTO Variable

Summary: in this tutorial, you will learn how to use the MySQL SELECT INTO variable  to
store query result in variables.

MySQL SELECT INTO Variable syntax

To store query (https://www.mysqltutorial.org/mysql-select-statement-query-data.aspx) result in one or


more variables (https://www.mysqltutorial.org/mysql-variables/) , you use the SELECT INTO
variable syntax:

SELECT
c1, c2, c3, ...
INTO
@v1, @v2, @v3,...
FROM
table_name
WHERE
condition;

In this syntax:

c1, c2, and c3 are columns or expressions that you want to select and store into the
variables.

@v1, @v2, and @v3 are the variables which store the values from c1, c2 and c3.

The number of variables must be the same as the number of columns or expressions in the
select list. In addition, the query must returns zero or one row.

If the query return no rows, MySQL issues a warning of no data and the value of the
variables remain unchanged.
In case the query returns multiple rows, MySQL issues an error. To ensure that the query
always returns maximum one row, you use the LIMIT 1 clause to limit the result set to a
single row.

MySQL SELECT INTO Variable examples

We will use the customers table in the sample database (https://www.mysqltutorial.org/mysql-


sample-database.aspx) for the demonstration.

MySQL SELECT INTO single variable example


The following statement gets the city of the customer with the number 103 and stores it in
the @city variable:

SELECT
city
INTO
@city
FROM
customers
WHERE
customerNumber = 103;

The following statement displays the content of the @city variable:


SELECT
@city;

MySQL SELECT INTO multiple variables example


To store values from the select list into multiple variables, you separate variables by
commas. For example, the following statement finds the city and country of the customer
number 103 and stores the data in two corresponding variables @city and @country:

SELECT
city,
country
INTO
@city,
@country
FROM
customers
WHERE
customerNumber = 103;

The following statement shows the contents of the @city and @country variables:

SELECT
@city,
@country;
MySQL SELECT INTO variable – multiple rows example
The following statement causes an error because the query returns multiple rows:

SELECT
creditLimit
INTO
@creditLimit
FROM
customers
WHERE
customerNumber > 103;

Here is the output:

Error Code: 1172. Result consisted of more than one row

To fix it, you use the LIMIT 1 (https://www.mysqltutorial.org/mysql-limit.aspx) clause as


follows:
SELECT
creditLimit
INTO
@creditLimit
FROM
customers
WHERE
customerNumber > 103
LIMIT 1;

In this tutorial, you have learned how to use the MySQL SELECT INTO variable syntax to
store result query result in one or more variables.

Copyright © 2020 by www.mysqltutorial.org. All Rights Reserved.

You might also like