Heim  >  Artikel  >  Datenbank  >  mysql存储过程使用select ... into语句为变量赋值范例

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

WBOY
WBOYOriginal
2016-06-07 15:33:312059Durchsuche

在MySQL存储过程中,可以使用SELECT …INTO语句对变量进行赋,该语句在数据库 中进行查询,并将得到的结果赋给变量。SELECT …INTO语句的语法式如下: SELECT col_name[,...] INTO var_name[,...] table_expr 代码如下:create procedure getMsg () Begin dec

在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(); 调用该存储过程以后,就会在控制台上输出这两个信息。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn