1

私は小さな関数を書きました:

CREATE TABLE states
    (`id` int, `name` varchar(2))
;

INSERT INTO states
    (`id`, `name`)
VALUES
    (1, 'md'),
    (2, 'tx'),
    (3, 'ma')
;

delimiter //

create function states_repeated (s varchar(2))
returns int
begin
  insert into sid select count(*) from states where states.name=s ;
  return sid ;
end//

delimiter ;

select states_repated('ma') ;

しかし、これは戻ります

ERROR 1146 (42S02): Table 'test.sid' doesn't exist

この値を返すにはどうすればよいですか?

4

2 に答える 2

2

このようなことを試して、

DECLARE _returnValue INT;
SET _returnValue = (select count(*) from states where states.state = s);
return _returnValue;

完全なコード

delimiter //

create function states_repeated (s varchar(2))
returns int
begin
    DECLARE _returnValue INT;
    SET _returnValue = (select count(*) from states where states.name = s);
    return _returnValue;
end//

delimiter ;
于 2012-10-17T23:29:28.920 に答える
0

mysql関数とストアドプロシージャは宣言された変数を返すだけだと思います

于 2012-10-17T23:30:48.950 に答える