将变量值分配给 mysql 中的输出参数

Assigning variable value to out parameter in mysql

两个插入语句都成功并在 table registrationregistration_header 上正确插入。我唯一的问题是,它没有通过 myOutParameter.

return 正确的 ID

它没有使用 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

不知道怎么回事

希望能帮到你。

提前致谢

架构:

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()