开发者

PostgreSQL: nested loop

Is it possible to have a loop within another loop? (nested loops) in postgres. Something similar to below:

WHILE rowCounter < totalRows LOOP

   开发者_运维知识库 FETCH NEXT FROM retailerIdCursor INTO retailerID;

    --FOR i IN 1..8 LOOP
    --WHILE i < 8 LOOP
    LOOP
        UPDATE sales_fact
        SET retailer_id = retailerID
        WHERE sales_id = rowCounter;

        EXIT WHEN i > 8;
    END LOOP;

    rowCounter = 1+ rowCounter;

END LOOP;


I fail to see why you cannot just write

UPDATE sales_fact
   SET retailer_id = retailerID
WHERE sales_id IN(1,2,3,4,5,6,7,8);

as Frank has suggested.

Depending on the statement that selects the retailerId, you might not even need the outer loop as well.


You can look at postgresql documentation. http://www.postgresql.org/docs/current/static/plpgsql-control-structures.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜