1

I need to create a table based on the last id inserted into another main table, for this I created the following stored procedure:

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

But the problem is that only inserts the first record and fails to create the table. Where am I failing?

2
  • Did it throw any error? What is the output of select p_return_code;? Or comment the sqlexception handler block and see what happens. Jul 19, 2012 at 17:28
  • I commented SQLException and have added: "SELECT p_return_code," at the end. But I do not return anything! only: "Time: 0.000ms Procedure Executed successfully Affected rows: 0"
    – csotelo
    Jul 19, 2012 at 17:54

4 Answers 4

5

Replace this line: SET p_campaign := LAST_INSERT_ID();

With this one: SELECT LAST_INSERT_ID() INTO p_campaign ;

0

if you use SET you need = instead of :=

0
0

Two things:

  1. Executing DROP will implicitly COMMIT your transaction.

  2. Try adding

    DEALLOCATE PREPARE stm;

after each EXECUTE statement.

3
  • I get the same result. The main problem is getting the last ID. I'm using the same stored procedure to insert directly other records directly to the latest ID but still does not work. Will be for the transaction?
    – csotelo
    Jul 19, 2012 at 15:53
  • 1
    If db_campaign is MyISAM, you can remove the START TRANSACTION;, COMMIT; and ROLLBACK;s, as they're effectively NOOPs. You could add some logging, something like INSERT INTO syslog SET script='myscript', line='lineno', message=CONCAT('last_insert_id()=', LAST_INSERT_ID()); Jul 19, 2012 at 22:01
  • Yeah you're right. Investigating the MyISAM database does not support transactions.
    – csotelo
    Jul 20, 2012 at 14:07
0

According to MySQL documentation of last_insert_id()

If a stored procedure executes statements that change the value of LAST_INSERT_ID(), the changed value is seen by statements that follow the procedure call.

Use something like:

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

Or:

SELECT max(id) INTO p_campaign FROM db_campaign;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.