Mysql在存储过程中使用LAST_INSERT_ID

时间:2012-07-19 15:29:22

标签: mysql stored-procedures last-insert-id

我需要根据插入另一个主表的最后一个id创建一个表,为此我创建了以下存储过程:

CREATE PROCEDURE `sp_create_campaign`
(
        IN  p_vName         VARCHAR(70),
        IN  p_iIdOper       INT(11),
        IN  p_iIdCount      INT(11),
        IN  p_iIdMoney      INT(11),
        IN  p_cPrefix       CHAR(2),
        IN  p_tComment      TINYTEXT,
        IN  p_iIdUser       VARCHAR(32),
        OUT p_return_code   TINYINT UNSIGNED
)
BEGIN

    DECLARE p_campaign INT(11);

    DECLARE exit handler for sqlexception
    BEGIN
        -- ERROR
            set p_return_code = 1;
            rollback;
    END;

    DECLARE exit handler for sqlwarning
    BEGIN
            -- WARNING
            set p_return_code = 2;
            rollback;
    END;

    START TRANSACTION;

        -- Campaign
        INSERT INTO `db_campaign` 
            (`vName`, `iIdOper`, `iIdCount`, `iIdMoney`, `cPrefix`, `tComment`, `iIdUser`, `dRegister`)
            VALUES
            (p_vName, p_iIdOper, p_iIdCount, p_iIdMoney, p_cPrefix, p_tComment, p_iIdUser, NOW());

        SET p_campaign := LAST_INSERT_ID();

        -- Sales
        SET @s = CONCAT('DROP TABLE IF EXISTS ', 'db_sale_', p_campaign);
        PREPARE stm FROM @s;
        EXECUTE stm;

        SET @x = CONCAT(
                'CREATE TABLE ',
                'db_sale_', p_campaign,
                "(
                `iIdSale`           INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
                `dDate`             DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
                `dSubtotal`         DECIMAL(7,2) UNSIGNED NOT NULL DEFAULT '00.00',
                `dTax`              DECIMAL(7,2) UNSIGNED NOT NULL DEFAULT '00.00',
                `dTotal`            DECIMAL(7,2) UNSIGNED NOT NULL DEFAULT '00.00',
                `iIdMoney`          INT(11) UNSIGNED NOT NULL,
                `iIdOper`           INT(11) UNSIGNED NOT NULL,
                `iIdBankCount`      INT(11) UNSIGNED NOT NULL,
                `iIdGroup`          INT(11) UNSIGNED NOT NULL,
                `iIdUser`           INT(11) UNSIGNED NOT NULL,
                `iIdUserReg`        VARCHAR(32) NOT NULL,
                `dRegister`         DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
                PRIMARY KEY (`iIdSale`)
                ) ENGINE=MyISAM DEFAULT CHARSET=utf8;");        

        PREPARE stm FROM @x;
        EXECUTE stm;

    COMMIT;

    -- SUCCESS
    set p_return_code = 0;

END

但问题是只插入第一条记录而无法创建表格。我在哪里失败?

4 个答案:

答案 0 :(得分:1)

替换此行:SET p_campaign := LAST_INSERT_ID();

这一个:SELECT LAST_INSERT_ID() INTO p_campaign ;

答案 1 :(得分:0)

如果您使用SET,则需要=而不是:=

答案 2 :(得分:0)

两件事:

  1. 执行DROP将隐含COMMIT您的交易。

  2. 尝试添加

    DEALLOCATE PREPARE stm;

  3. 每个EXECUTE语句后

答案 3 :(得分:0)

根据last_insert_id()

的MySQL文档
  

如果存储过程执行更改LAST_INSERT_ID()值的语句,则更改的值将由过程调用之后的语句看到。

使用类似:

SELECT id INTO p_campaign FROM db_campaign ORDER BY id DESC LIMIT 1;

或者:

SELECT max(id) INTO p_campaign FROM db_campaign;