Sql FOR循环处的语法错误

Sql FOR循环处的语法错误,sql,postgresql,plpgsql,Sql,Postgresql,Plpgsql,我有这个功能。它还不完整,也没有做任何有意义的事情,但在写作过程中,我遇到了以下错误: 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

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

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;
部分。

您错过了

END IF;
部分