简体   繁体   English

PostgreSQL - 遍历查询结果

[英]PostgreSQL - Iterate over results of query

I am creating a function in pgsql script language, and what I want to do in this point is iterate over the results of a query and for each row do something specific.我正在用 pgsql 脚本语言创建一个 function,在这一点上我想做的是迭代查询的结果,并为每一行做一些特定的事情。 My current try is the following, where temprow is declared as temprow user_data.users%rowtype .我目前的尝试如下,其中temprow被声明为temprow user_data.users%rowtype The code in question is the following:有问题的代码如下:

FOR temprow IN
        SELECT * FROM user_data.users ORDER BY user_seasonpts DESC LIMIT 10
    LOOP
        SELECT user_id,user_seasonpts INTO player_idd,season_ptss FROM temprow;
        INSERT INTO user_data.leaderboards (season_num,player_id,season_pts) VALUES (old_seasonnum,player_idd,season_ptss);
    END LOOP;  

However I get the following error from this: ERROR: relation "temprow" does not exist .但是,我从中得到以下错误: ERROR: relation "temprow" does not exist If it's clear what I want to be done, could you point to me the right way to do it?如果很清楚我想做什么,你能指出我正确的方法吗?

temprow is a record variable which is bound in turn to each record of the first SELECT . temprow是一个记录变量,它依次绑定到第一个SELECT每个记录。

So you should write:所以你应该写:

FOR temprow IN
        SELECT * FROM user_data.users ORDER BY user_seasonpts DESC LIMIT 10
    LOOP
        INSERT INTO user_data.leaderboards (season_num,player_id,season_pts) VALUES (old_seasonnum,temprow.userd_id,temprow.season_ptss);
    END LOOP;

This loop could be further simplified as a single query:这个循环可以进一步简化为单个查询:

INSERT INTO user_data.leaderboards (season_num,player_id,season_pts)
SELECT old_seasonnum,player_idd,season_ptss FROM user_data.users ORDER BY user_seasonpts DESC LIMIT 10

A function that loop through the select and use loop item values to filter and calculate other values,循环选择并使用循环项值来过滤和计算其他值的函数,

CREATE FUNCTION "UpdateTable"() RETURNS boolean
    LANGUAGE plpgsql
AS
$$
DECLARE
    TABLE_RECORD RECORD;
    BasePrice NUMERIC;
    PlatformFee NUMERIC;
    MarketPrice NUMERIC;
    FinalAmount NUMERIC;
BEGIN
    FOR TABLE_RECORD IN SELECT * FROM "SchemaName1"."TableName1" -- can select required fields only

        LOOP
            SELECT "BasePrice", "PlatformFee" INTO BasePrice, PlatformFee
            FROM "SchemaName2"."TableName2" WHERE "UserID" = TABLE_RECORD."UserRID";

            SELECT "MarketPrice" / 100 INTO MarketPrice FROM "SchemaName3"."TableName3" WHERE "DateTime" = TABLE_RECORD."DateTime";

            FinalAmount = TABLE_RECORD."Qty" * (BasePrice + PlatformFee - MarketPrice);

            UPDATE "SchemaName1"."TableName1" SET "MarketPrice" = MarketPrice, "Amount" = CFDAmount
            WHERE "ID" = CFD_RECORD."ID"; -- can update other schema tables also
        END LOOP;

    RETURN TRUE;
END
$$;

For future reference, I want to emphasise Thushara comment on the accepted answer以供将来参考,我想强调Thushara对已接受答案的评论

On Postgres@12 the following would work:在 Postgres@12 上,以下将起作用:

DO $$
DECLARE temprow RECORD;
BEGIN FOR temprow IN
    SELECT * FROM user_data.users ORDER BY user_seasonpts DESC LIMIT 10
  LOOP
    INSERT INTO user_data.leaderboards (season_num,player_id,season_pts) VALUES (old_seasonnum,temprow.userd_id,temprow.season_ptss);
  END LOOP;
END $$

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

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