У меня есть функция, предназначенная для присвоения оценок определенным школам в таблице simulated_records, которая основана на количестве студентов и вероятности того, что каждый студент получит конкретную оценку для этой школы. У меня уже есть вся эта вероятность, но я получаю ошибки при попытке обновить таблицу с правильными оценками. Я не знаю, обновляюсь ли я неправильно или помещаю обновление в правильную область.Обновление таблицы со случайным классом
CREATE FUNCTION assigngrades(probs numeric[], school text, record_id bigint) RETURNS void AS $$
DECLARE
num_grades integer ARRAY[6];
found_school school_probs%ROWTYPE;
num_students simulated_records%ROWTYPE;
random_record simulated_records%ROWTYPE;
num_students_int bigint;
BEGIN
SELECT INTO found_school * FROM school_probs WHERE school_code = record_id;
SELECT INTO num_students * FROM simulated_records WHERE school = school_name;
SELECT COUNT(*) INTO num_students_int FROM simulated_records WHERE school = school;
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 = '-' THEN
random_record.grade = 'A';
END IF;
UPDATE simulated_records.grade = random_record.grade;
END Loop;
FOR i IN 1..num_grades[2] LOOP
SELECT INTO random_record * FROM simulated_records WHERE school = school_name ORDER BY RANDOM() LIMIT 1;
IF random_record.grade = '-' THEN
random_record.grade := 'A-';
END IF;
UPDATE simulated_records.grade = random_record.grade;
END Loop;
FOR i IN 1..num_grades[3] LOOP
SELECT INTO random_record * FROM simulated_records WHERE school = school_name ORDER BY RANDOM() LIMIT 1;
IF random_record.grade = '-' THEN
random_record.grade := 'B+';
END IF;
UPDATE simulated_records.grade = random_record.grade;
END Loop;
FOR i IN 1..num_grades[4] LOOP
SELECT INTO random_record * FROM simulated_records WHERE school = school_name ORDER BY RANDOM() LIMIT 1;
IF random_record.grade = '-' THEN
random_record.grade := 'B';
END IF;
UPDATE simulated_records.grade = random_record.grade;
END Loop;
FOR i IN 1..num_grades[5] LOOP
SELECT INTO random_record * FROM simulated_records WHERE school = school_name ORDER BY RANDOM() LIMIT 1;
IF random_record.grade = '-' THEN
random_record.grade := 'C';
END IF;
UPDATE simulated_records.grade = random_record.grade;
END Loop;
FOR i IN 1..num_grades[6] LOOP
SELECT INTO random_record * FROM simulated_records WHERE school = school_name ORDER BY RANDOM() LIMIT 1;
IF random_record.grade = '-' THEN
random_record.grade = 'D';
END IF;
UPDATE simulated_records.grade = random_record.grade;
END Loop;
END;
$$
LANGUAGE plpgsql
Честно говоря, я просто не уверен, как правильно его обновить. Что мне делать?