Tater Of Tots
07/20/2022, 6:38 PMstudents table and then the function also gets an array of phone numbers and I need to add them to the phones table with the new student_id. I have gotten far enough that it add the student and returns the id then puts the id in a variable. The part I'm stuck on is the syntax for taking this array of phone numbers and and adding a row for each number along with the student_id. Heres what I have so far
create or replace function insert_student(
first_name text,
last_name text,
grade text,
dob date,
phones phone_type[]
)
RETURNS setof students
AS $$
declare
student_id int8;
begin
INSERT INTO students
(first_name, last_name, grade, dob) values (first_name, last_name, grade, dob)
returning id INTO student_id;
INSERT INTO phones
(phone_number, student_id) values
(phones, student_id); --THIS IS THE PART I'M STUCK ON
RETURN query select * from students where students.id = student_id;
end;
$$ language plpgsql;Needle
07/20/2022, 6:38 PMTater Of Tots
07/20/2022, 6:39 PMphone_type with this line create type phone_type as (phone_number text);silentworks
07/20/2022, 7:05 PMstudent_id which is also a column name. You need to change the variable name to something else.Tater Of Tots
07/20/2022, 7:06 PMTater Of Tots
07/20/2022, 7:06 PMTater Of Tots
07/20/2022, 7:07 PMcreate or replace function insert_student(
first_name text,
last_name text,
grade text,
dob date,
phones phone_type[]
)
RETURNS setof students
AS $$
declare
student_id_var int8;
begin
INSERT INTO students
(first_name, last_name, grade, dob) values (first_name, last_name, grade, dob)
returning id INTO student_id_var;
INSERT INTO phones
(phone_number, student_id) values
(phones, student_id_var); --THIS IS THE PART I'M STUCK ON
RETURN query select * from students where students.id = student_id_var;
end;
$$ language plpgsql;Tater Of Tots
07/20/2022, 7:08 PM