mysql - 使用两个 OUT 变量的 Sproc 无法编译

标签 mysql stored-procedures

我正在使用 Mysql 学习存储过程,但中央查询没有“编译”,我收到从“count(language) into”开始的语法警告。

我正在发送一个 var,并期待返回 2 个、所有语言的字符串和一个 int 计数。

drop procedure if exists langcnt;
delimiter &&
create procedure langcnt(
  IN ctry char(20)
, OUT langs varchar(100)
, OUT cnt int(3) 
)
begin

select group_concat(language) into langs 
, count(language) into cnt 
from countrylanguage where countrycode = ctry;

end &&
delimiter ;

call langcnt('fra',  @langs, @cnt);
select @langs, @cnt;

隔离后,以下内容会按预期工作:

select group_concat(language) as langs, 
count(language) as cnt 
from countrylanguage where countrycode = 'fra';

谁能赐教,我违反了什么规则?

编辑:某人:

select group_concat(language)  
, count(language) into langs,  cnt 
from countrylanguage where countrycode = ctry;

FTW,为@Bill Karwin 干杯

最佳答案

SELECT 不会在每一列之后使用 INTO 子句。

INTO 子句位于选择列表的末尾,您可以在其中命名多个变量,其顺序与选择列表的列相同。

select group_concat(language) 
, count(language) into langs, cnt 
from countrylanguage where countrycode = ctry;

https://dev.mysql.com/doc/refman/5.6/en/select-into.html说:

The SELECT syntax description (see Section 13.2.9, “SELECT Syntax”) shows the INTO clause near the end of the statement. It is also possible to use INTO immediately following the select_expr list.

关于mysql - 使用两个 OUT 变量的 Sproc 无法编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25753187/

相关文章:

java - 像 DBMS 一样快速计算笛卡尔积

php - 如何使用一个存储过程从单独的表中返回 mysql 列?

java - 从 Oracle (10g) 存储过程调用 Web 服务

c# - 在 SQL Server 2008 中调用存储过程时超时

php - mysql 在最后一条之前插入记录

mysql - 从 ibdata1、ib_logfile0、ib_logfile1 和 .frm 文件恢复数据库

MySQL Escape查询结果中的双引号

MySQL如何选择每个组的第一行计数

MySQL 存储过程的值中带有连字符

sql - 写函数时PostgreSQL sql函数语法错误