存储过程 - 循环结果并将它们存储在临时表中

时间:2014-01-07 12:20:58

标签: mysql sql stored-procedures

我遇到的问题是Mysql工作台在“DELARE tempOrder_id int;”附近报告了一个SQL语法错误,声明带有红色下划线但是我似乎无法看到错误,也无法测试这个但要么如果你发现任何其他错误或更有效的方式来运行这个,请说。我应该很容易从查询中获取id然后运行带有该id的新查询并将其存储在表中以便我可以返回它

-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$

CREATE PROCEDURE `DLP_orders ` ()
BEGIN
    Create temporary table if not exists MyReturnTable
        (id int, order_id int, total_tickets int, product_id int, product_name varchar(255), PRIMARY KEY (id)) ENGINE=MEMORY;

    DECLARE tempOrder_id int;
    DECLARE order_Cursor;

    SELECT o.id FROM orders as o
        INNER JOIN orders_items as oi
            ON o.id = oi.order_id
        INNER JOIN contracts as c
            ON c.id = oi.contract_id
    WHERE (o.created BETWEEN '2013-01-01 00:00:00' AND '2013-02-02 23:59:59' OR o.updated BETWEEN '2013-01-01 00:00:00' AND '2013-02-02 23:59:59') AND (c.supplier_id = 95 || c.supplier_id = 255);

    OPEN order_Cursor;
    FETCH NEXT FROM order_Cursor INTO @tempOrder_id;

    WHILE @@FETCH_STATUS = 0
        BEGIN
            INSERT INTO MyReturnTable (order_id, total_tickets,  product_id, product_name)
                SELECT o.id, oi.total_tickets, p.id, p.name FROM orders as o
                    INNER JOIN orders_items as oi
                        ON o.id = oi.order_id
                    INNER JOIN products as p
                        ON p.id = oi.product_id
                    INNER JOIN contracts as c
                        ON c.id = oi.contract_id
                WHERE o.id = @tempOrder_id;

            FETCH NEXT FROM order_Cursor INTO @tempOrder_id;
        END;

    CLOSE order_Cursor;
    DEALLOCATE order_Cursor;

    SELECT * FROM MyReturnTable;
END$$

1 个答案:

答案 0 :(得分:1)

经过一些调整后,这是我的解决方案

-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$

CREATE PROCEDURE `DLP_orders` ()
BEGIN
    DECLARE tempOrder_id int;
    DECLARE order_Cursor CURSOR FOR
    SELECT o.id FROM orders as o
        INNER JOIN orders_items as oi
            ON o.id = oi.order_id
        INNER JOIN contracts as c
            ON c.id = oi.contract_id
    WHERE (o.created BETWEEN '2013-01-01 00:00:00' AND '2013-02-02 23:59:59' OR o.updated BETWEEN '2013-01-01 00:00:00' AND '2013-02-02 23:59:59') AND (c.supplier_id = 95 || c.supplier_id = 255);

    OPEN order_Cursor;
    FETCH NEXT FROM order_Cursor INTO tempOrder_id;

    WHILE @@FETCH_STATUS = 0 DO
            INSERT INTO MyReturnTable (order_id, total_tickets,  product_id, product_name)
                SELECT o.id, oi.total_tickets, p.id, p.name FROM orders as o
                    INNER JOIN orders_items as oi
                        ON o.id = oi.order_id
                    INNER JOIN products as p
                        ON p.id = oi.product_id
                    INNER JOIN contracts as c
                        ON c.id = oi.contract_id
                WHERE o.id = @tempOrder_id;

            FETCH NEXT FROM order_Cursor INTO tempOrder_id;
    END WHILE;

    CLOSE order_Cursor;

    SELECT * FROM MyReturnTable;
END