Reputation: 193
I have this function. It's not complete and doesn't do anything meaningful yet, but in the process of writing I've been getting this error:
ERROR: syntax error at or near "LOOP"
LINE 26: END LOOP;
Could anyone look at my code and see why?? My for loop syntax looks fine to me.
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
Upvotes: 0
Views: 5619