Sproc using two OUT vars won't compile

12 Views Asked by At

I am learning about sprocs with Mysql, and the central query does not "compile", I get a syntax warning starting at "count(language) into ".

I'm sending in one var, and expecting 2 back, a string of all languages, and an int count.

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;

When isolated, the following does work as expected:

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

Can anyone enlighten me, what rule have I broken?

EDIT: SB:

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

FTW, cheers @Bill Karwin

1

There are 1 best solutions below

1
On BEST ANSWER

SELECT does not take an INTO clause after each column.

The INTO clause goes at the end of the select-list, where you can name multiple variables, in the same order as the columns of the select-list.

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 says:

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.