错误:缺少" LOOP"在SQL表达式的末尾

时间:2017-03-19 20:25:10

标签: postgresql plpgsql

  

错误:缺少" LOOP"在SQL表达式的末尾   CONTEXT:编译PL / pgSQL函数" player_height_rank"靠近第9行

这是我的代码:

CREATE OR REPLACE FUNCTION player_height_rank (irstname VARCHAR, lastname VARCHAR) RETURNS int AS $$ 

DECLARE

    rank INTEGER := 0;
    offset INTEGER := 0;
    tempValue INTEGER := NULL;
    r record;
BEGIN

FOR r IN SELECT ((p.h_feet * 30.48) + (p.h_inches * 2.54)) AS height, p.firstname, p.lastname
    FROM players p
    WHERE p.firstname = $1 AND p.lastname = $2;
    ORDER BY ((p.h_feet * 30.48) + (p.h_inches * 2.54)) DESC, p.firstname, p.lastname

    LOOP

        IF r.height = tempValue then
            offset := offset + 1;
        ELSE
            rank := rank + offset + 1;
            offset := 0;
            tempValue := r.height;
        END IF;

        IF r.lastname = $4 AND r.lastname = $3 THEN
            RETURN rank;
        END IF;
    END LOOP;

    -- not in DB
    RETURN -1;

    END;
    $$ LANGUAGE plpgsql;

1 个答案:

答案 0 :(得分:1)

在你的WHERE子句中,分号是多余的

WHERE p.firstname = $1 AND p.lastname = $2; -- delete semicolon

更正该部分,然后重试。