Question

I try to do this

CREATE FUNCTION getOneCentOrderIds (s text) RETURNS text
BEGIN
    DECLARE no_more_orders, ent_id INT default 0;
    DECLARE ids text;
    DECLARE orders_cur CURSOR FOR SELECT entity_id FROM sales_flat_order WHERE total_due = 0.01;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_orders = 1;
    OPEN orders_cur;
        FETCH NEXT FROM orders_cur INTO ent_id;
        REPEAT
            SET ids = CONCAT(ids, ', ', ent_id);
            FETCH orders_cur INTO ent_id;
        UNTIL no_more_orders END REPEAT;
    CLOSE orders_cur;
    RETURN ids;
END$

but I get null when I execute the function.

If I simply remove concat and leave SET ids = ent_id I get the last id in cursor, as expected.

How should I do the concatenation ?

Était-ce utile?

La solution

The concat() function returns NULL if any of its arguments are NULL. Try

DECLARE ids text DEFAULT '';

which will make sure the first call to CONCAT has no NULL arguments.

Autres conseils

Instead of creating a function, the above can simple be done in a query as

SELECT group_concat(entity_id) FROM sales_flat_order WHERE total_due = 0.01;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top