When I execute this query with Show
asked Apr 9, 2012 at 7:48
Query MasterQuery Master 6,8395 gold badges32 silver badges55 bronze badges 1 Surround that select with parentheses.
answered Apr 9, 2012 at 7:50
Sergio TulentsevSergio Tulentsev 222k42 gold badges364 silver badges359 bronze badges 9 Additionally,
if you want to set multiple variables at once by one query, you can use the other syntax for setting variables which goes like this: A practical example:
answered Apr 25, 2014 at 0:19
YirkhaYirkha 11.7k5 gold badges39 silver badges52 bronze badges use this
tested and workes fine... answered Apr 27, 2014 at 22:45
Aman MauryaAman Maurya 1,31512 silver badges26 bronze badges 0
AgeDeO 3,1342 gold badges22 silver badges57 bronze badges answered Dec 17, 2019 at 10:37
0 To store query result in a variable with MySQL, use the SET command. The syntax is as follows − SET @anyVariableName = ( yourQuery); To understand the above concept, let us create a table. The following is the query to create a table − mysql> create table QueryResultDemo −> ( −> Price int −> ); Query OK, 0 rows affected (0.59 sec) Now let us insert some records into the table. The following is the query to insert records − mysql> insert into QueryResultDemo values(100); Query OK, 1 row affected (0.17 sec) mysql> insert into QueryResultDemo values(20); Query OK, 1 row affected (0.13 sec) mysql> insert into QueryResultDemo values(200); Query OK, 1 row affected (0.10 sec) mysql> insert into QueryResultDemo values(80); Query OK, 1 row affected (0.15 sec) Display all records from the table with the help of select statement. The query to display all records is as follows − mysql> select *from QueryResultDemo; The following is the output − +-------+ | Price | +-------+ | 100 | | 20 | | 200 | | 80 | +-------+ 4 rows in set (0.00 sec) Now you can set the query result in a variable with the help of SET command. The query is as follows. mysql> Set @TotalPrice = (select sum(Price) from QueryResultDemo); Query OK, 0 rows affected (0.00 sec) Check what is the value stored in variable “TotalPrice”, using the SELECT statement − mysql> select @TotalPrice; The following is the output − +-------------+ | @TotalPrice | +-------------+ | 400 | +-------------+ 1 row in set (0.00 sec)
Updated on 30-Jul-2019 22:30:24
Can we store query in variable in SQL?Yup, this is possible of course.
Can we store value in variable in MySQL?You can store a value in a user-defined variable in one statement and refer to it later in another statement. This enables you to pass values from one statement to another. User variables are written as @ var_name , where the variable name var_name consists of alphanumeric characters, . , _ , and $ .
How do I store select query results in a table?Exporting query results to a database. To open the Export Query Results wizard, select File > Export. ... . Select Database. ... . From the Source list, select the query result set that you want to export. ... . Specify the database where you want to save the query results data in the Database field.. How do I store select query results in variable in SQL Developer?PL/SQL SELECT INTO examples. First, declare a variable l_customer_name whose data type anchors to the name columns of the customers table. ... . Second, use the SELECT INTO statement to select value from the name column and assign it to the l_customer_name variable.. Third, show the customer name using the dbms_output.. |