简体   繁体   English

在存储过程中创建临时表

[英]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. 我试图在过程中创建临时表,但是当我尝试执行过程时,我却以某种方式不断出错(在DECLARE l_coin_base_id INT UNSIGNED处为#1064)。 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. 如果我尝试在一条语句中执行CREATE TEMPORARY TABLE部分,则无法创建临时表及其正确创建的表。 Google doesnt know it either. 谷歌也不知道。 (This is also my first time using join so maybe there is my mistake?). (这也是我第一次使用join,所以也许是我的错误?)。

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. 因此,在程序开始时声明所有变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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