2016-08-04 3 views
0

insert 문이 모두 성공적으로 수행되어 테이블 registrationregistration_header에 올바르게 삽입됩니다. 내 유일한 문제는 myOutParameter 통해 올바른 ID를 반환하지 않습니다.mysql의 out 매개 변수에 변수 값 할당

그것은 SET myOutParameter = @var_registrationId

내 구문이 잘못 사용 @var_registrationId의 값을 따기 아니에요? 내가 그것을 설정할 수 있다는 것을 알고있다 SET

CREATE DEFINER=`root`@`localhost` PROCEDURE `register`(IN parameter1 INT, IN parameter2 INT, OUT myOutParameter INT) 

     BEGIN 

     DECLARE var_registrationId INT; 
     DECLARE EXIT HANDLER FOR sqlexception 
      BEGIN 
       ROLLBACK; 
       RESIGNAL; 
      END; 

     START TRANSACTION; 
     -- first insert to registration table which generates a Primary key auto increment id 
     INSERT INTO registration(col1,col2) VALUES (parameter1, parameter2); 

     SELECT LAST_INSERT_VALUE() INTO var_registrationId; -- id of insert on registration table 

     insert into registration_header(registrationId,column) 
     VALUES(@var_registrationId,parameter1); 

     -- the next statement is not assigning the value of var_registrationId to the myOutParameter using SET 

    SET myOutParameter = @var_registrationId; -- this isn't working and returns 0 

COMMIT; 

    END 

나는 무엇이 잘못 된 것인지 모른다.

도와 주시면 감사하겠습니다. 사전에

감사

+0

'var_registrationId'는'@ var_registrationId'와 다릅니다. – Blank

+0

지금 이해하십니까? – Drew

+0

@Drew @ 기호를 제거했습니다. 사용 방법 및 사용시기와 @ 기호가 어떻게 다른지 혼란스러워합니다. – p3ace

답변

1

스키마 :

create schema blahx8; -- create a new db so as not to screw yours up 
use blahx8; -- use the new db 

-- drop table if exists registration; 
create table registration 
( id int auto_increment primary key, 
    col1 int not null, 
    col2 int not null 
); 

-- drop table if exists registration_header; 
create table registration_header 
( id int auto_increment primary key, 
    registrationId int not null, 
    `column` int not null -- pretty bad column name. Use back-ticks 
); 

저장된 프로 시저 :

DROP PROCEDURE IF EXISTS `register`; 
DELIMITER $$ 
CREATE DEFINER=`root`@`localhost` PROCEDURE `register` 
( IN parameter1 INT, 
    IN parameter2 INT, 
    OUT myOutParameter INT 
) 
BEGIN 

    DECLARE var_registrationId INT; 
    DECLARE EXIT HANDLER FOR sqlexception 
    BEGIN 
     ROLLBACK; 
     RESIGNAL; 
    END; 

    START TRANSACTION; 
    -- first insert to registration table which generates a Primary key auto increment id 
    INSERT INTO registration(col1,col2) VALUES (parameter1, parameter2); 
    SELECT LAST_INSERT_ID() INTO var_registrationId; -- id of insert on registration table 

    insert into registration_header(registrationId,`column`) 
    VALUES(registrationId,parameter1); 

    SET myOutParameter = var_registrationId; -- This is now happy 
    COMMIT; 
    SET @still_Alive=7; -- watch this thing !! Be careful 
END$$ 
DELIMITER ; 

테스트 :

SET @thisThing=-1; 
CALL register(7,8,@thisThing); 
select @thisThing; -- 1 
CALL register(7,8,@thisThing); 
select @thisThing; -- 2 
CALL register(7,8,@thisThing); 
select @thisThing; -- 3 

select @still_Alive; -- 7 
-- yikes, be carefull with User Variables. They are connection-based 
-- still alive out here outside of stored proc (unlike Local Vars) 

정리 :

drop schema blahx8; -- drop new db, poof gone 

로컬 변수 (DECLARE의)는 거의 비슷한 사용자 변수 (@ 부호 포함)와 같지 않습니다.

또한 LAST_INSERT_ID()을 수정했습니다.

+0

다시 감사드립니다. 철자가 틀렸어. ID()가 있어야 했어. = P 프로시 저는 실제로 여러 줄을 가지고있다. 어쨌든, 나는 귀하의 답변을 참조로 활용할 것입니다. 나는 Enrollment System에서 일하고 있는데 프론트 엔드와 백엔드 모두에게 감사해야만합니다. – p3ace