2016-08-04 2 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のプロシージャは実際にいくつかの行を持っている必要があります。とにかく、あなたの答えを参考にしてみましょう。私は登録システムに取り組んでいます。フロントエンドとバックエンドの両方に感謝しなければなりません。 – p3ace

関連する問題