FOR 循环语法错误

Syntax error at FOR LOOP

我有这个功能。它不完整,也没有做任何有意义的事情,但在写作过程中我遇到了这个错误:

ERROR:  syntax error at or near "LOOP"
LINE 26:  END LOOP;

谁能看看我的代码,看看为什么??我的 for 循环语法看起来不错。

CREATE FUNCTION assignGrades(prob numeric[], school_name text, school_id bigint) RETURNS void AS $$
DECLARE
    num_grades integer ARRAY[6];
    found_school school_probs%ROWTYPE;
    num_students simulated_records%ROWTYPE;
    num_students_int bigint;
    random_record simulated_records%ROWTYPE;


BEGIN
    SELECT INTO found_school * FROM school_probs WHERE school_code = school_id;--select the prob dist
    SELECT INTO num_students * FROM simulated_records WHERE school = school_name;
    SELECT COUNT(*) INTO num_students_int FROM simulated_records WHERE school = school_name;
    num_grades[1] = num_students_int*found_school.probs[1];
    num_grades[2] = num_students_int*found_school.probs[1];
    num_grades[3] = num_students_int*found_school.probs[1];
    num_grades[4] = num_students_int*found_school.probs[1];
    num_grades[5] = num_students_int*found_school.probs[1];
    num_grades[6] = num_students_int*found_school.probs[1];

    FOR i IN 1..num_grades[1] LOOP

        SELECT INTO random_record * FROM simulated_records WHERE school = school_name ORDER BY RANDOM() LIMIT 1;
        IF random_record.grade IS NULL THEN
            random_record.grade = 'A';
    END LOOP;--syntax error here


END;
$$
LANGUAGE plpgsql

您错过了

END IF;

部分.