将值添加到存储过程中的变量中

时间:2017-09-12 13:04:19

标签: mysql stored-procedures

从table1中选择column_1,column_2;

我需要将column_1和column_2值放入变量中,这样我才能在插入另一个表之前进行一些操作。

感谢您的所有帮助

2 个答案:

答案 0 :(得分:0)

DECLARE @VAR1 INT
DECLARE @VAR2 INT

SELECT 
@VAR1 = column_1,
@VAR2 = column_2
from Table

您可以执行以下操作来设置参数并执行您喜欢的操作。

答案 1 :(得分:0)

你还没有真正提供太多信息,但也许会有这样的事情

drop procedure if exists p;
delimiter $$

create procedure p ()
BEGIN 
declare cat_name varchar(100); 
declare cat_id int; 
select  id,username into cat_id,cat_name
 from users where id= 1; 
select cat_name,cat_id from dual;

END  $$
delimiter ;

用户看起来像这样

MariaDB [sandbox]> select * from users;
+----+----------+--------------+--------+---------------------+---------------------+--------+-------------+-----------+
| id | userName | photo        | status | ts                  | events_participated | fb_uid | Column_name | post_type |
+----+----------+--------------+--------+---------------------+---------------------+--------+-------------+-----------+
|  1 | John     | john.png     |      1 | 2016-12-08 13:14:24 |                   1 |      1 | 12346       | photo     |
|  2 | Jane     | jane.png     |      1 | 2016-12-08 13:14:24 |                NULL |      2 | 12346       | photo     |
|  3 | Ali      |              |      1 | 2016-12-08 13:14:24 |                NULL |      3 | 12346       | photo     |
|  6 | Bruce    | bruce.png    |      1 | 2016-12-08 13:14:24 |                NULL |      6 | 12346       | photo     |
|  7 | Martha   |              |      1 | 2016-12-08 13:14:24 |                NULL |      7 | 12346       | photo     |
|  8 | Sidney   |              |      1 | 2016-12-08 13:14:24 |                NULL |      8 | 12346       | photo     |
| 10 | Charlie  | charlie.png  |      1 | 2016-12-08 13:14:24 |                NULL |     10 | 12346       | content   |
| 12 | Elisa    |              |      1 | 2016-12-08 13:14:24 |                NULL |     12 | 12346       | content   |
| 14 | Samantha | samantha.png |      1 | 2016-12-08 13:14:24 |                NULL |     14 | 12346       | content   |
| 15 | Hannah   | hannah.png   |      1 | 2016-12-08 13:14:24 |                NULL |     15 | 12346       | content   |
| 16 | Hannah   |              |      1 | 2016-12-08 13:14:24 |                NULL |     16 | 12346       | content   |
| 17 | Kevin    | kevin1.png   |      1 | 2016-12-08 13:14:24 |                NULL |     17 | 12346       | content   |
| 18 | Kevin    | kevin2.png   |      1 | 2016-12-08 13:14:24 |                NULL |     18 | 12346       | content   |
| 19 | Ruth     |              |      1 | 2016-12-08 13:14:24 |                NULL |     19 | 12346       | content   |
| 20 | xyz      | NULL         |   NULL | 2017-08-18 12:54:40 |                NULL |   NULL | NULL        | content   |
+----+----------+--------------+--------+---------------------+---------------------+--------+-------------+-----------+
15 rows in set (0.01 sec)

调用结果时

MariaDB [sandbox]> call p();
+----------+--------+
| cat_name | cat_id |
+----------+--------+
| John     |      1 |
+----------+--------+
1 row in set (0.01 sec)