2012-10-17 2 views
1

MySQL의 함수에서 값을 반환 :난 작은 기능 썼다

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 

을 반환?

+0

변경 INSERT를 반환,이 같은 시도 *)는 state.state가 s 인 상태에서 sid로; – SYNCRo

답변

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 ; 
0

내가 MySQL의 기능과 저장 프로 시저를 생각하는 전체 코드는 단지 (삽입 수가`에서 SELECT로 선언 된 변수