简体   繁体   中英

Creating temporary Tables in stored Proceduresmysql

this is my first post here, so im sorry for any mistakes or missing information you are expecting. Im trying to create a Temporary Table in a procedure but somehow i keep getting errors (#1064 at DECLARE l_coin_base_id INT UNSIGNED ), when i try to implement the procedure. I removed the whole Create temporary table part and it worked, so i believe my mistake is in this part.

DELIMITER //
CREATE PROCEDURE UPDATE_HISTORY()`

MODIFIES SQL DATA
BEGIN
    DECLARE l_c_pair INT DEFAULT (SELECT COUNT(*) + 1 FROM pairList); 
    DECLARE i INT DEFAULT 1; 
    DECLARE l_now INT UNSIGNED DEFAULT UNIX_TIMESTAMP() - 300;`

    CREATE TEMPORARY TABLE temp_history ENGINE = MEMORY AS(
SELECT
    active_pairList.base,
    active_pairList.peer,
    (active_history.highest_ask + active_history.highest_bid) / 2 AS price,
    active_history.buy_volume,
    active_history.sell_volume,
    active_history.trade_volume
FROM
    active_history
INNER JOIN
    active_pairList
ON
    active_history.sell_volume IS NOT NULL 
    AND active_history.buy_volume IS NOT NULL
    AND active_history.trade_volume IS NOT NULL
    AND active_history.timeStamp > l_now); 
    DECLARE l_coin_base_id INT UNSIGNED; 
    DECLARE l_coin_peer_id INT UNSIGNED; 
    DECLARE l_avg_price FLOAT UNSIGNED; 
    DECLARE l_sum_buy_volume FLOAT UNSIGNED; 
    DECLARE l_sum_sell_volume FLOAT UNSIGNED; 
    DECLARE l_sum_trade_volume FLOAT UNSIGNED; 
-- doing smth. --
END //
DELIMITER ;

If i try to execute the CREATE TEMPORARY TABLE part in a statement, im able to create the temporary table and its created correctly. Google doesnt know it either. (This is also my first time using join so maybe there is my mistake?).

I would be really thankfull for any help.

You can't define variable in middle of the procedure. It should be after BEGIN. So declare all variables in beginning of procedure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM