现在的位置: 首页 > 综合 > 正文

mysql存储过程使用select … into语句为变量赋值范例

2018年01月24日 ⁄ 综合 ⁄ 共 608字 ⁄ 字号 评论关闭

在MySQL存储过程中,可以使用SELECT …INTO语句对变量进行赋值,该语句在数据库中进行查询,并将得到的结果赋值给变量。SELECT
…INTO语句的语法格式如下:

SELECT col_name[,...] INTO var_name[,...] table_expr

代码如下:
create procedure getMsg  
()  
Begin 
  declare v_title varchar(30);  
  declare v_content varchar(100);  
  select title,content into v_title,v_content from news where artId=333;  
End  

将变量值返回给调用者

在存储过程中定义的变量,经过一系列的处理之后,结果值可能需要返回给存储过程调用者。那么如何返回呢?方便的做法是使用SELECT语句将变量作为结果集返回,因此,在上面一段代码的基础上,加上一句:

create procedure getMsg ()  
Begin 
  declare v_title varchar(30);  
  declare v_content varchar(100);  
  select title,content into v_title,v_content from news where artId=333;  
  select v_title,v_content;  
End

这样,执行call getMsg(); 调用该存储过程以后,就会在控制台上输出这两个信息。

抱歉!评论已关闭.