Why does this SQL function have side effect?

Viewed 69

In Database System Concepts:

– – Registers_a student after ensuring classroom capacity is_not exceeded
– – Returns_0 on success, and -1 if capacity is_exceeded.
create function registerStudent(
  in s_id varchar(5),
  in s_courseid varchar (8),
  in s_secid varchar (8),
  in s_semester varchar (6),
  in s_year numeric (4,0),
  out errorMsg varchar(100)
returns_integer
begin
  declare currEnrol int;
  select count(*) into currEnrol
    from takes
    where course id = s_courseid and sec id = s_secid
    and semester = s_semester and year = s_year;
  declare limit int;
  select capacity into limit
    from classroom natural join section
    where course id = s_courseid and sec id = s_secid
    and semester = s_semester and year = s_year;
  if (currEnrol < limit)
  begin
    insert into takes values
      (s_id, s_courseid, s_secid, s_semester, s_year, null);
    return(0);
  end
  – – Otherwise, section capacity limit already reached
  set errorMsg = ’Enrollment limit reached for course ’ || s_courseid
    || ’ section ’ || s_secid;
  return(-1);
end;

Figure 5.7 Procedure to register a student for a course section.

The book has both function and procedure concepts. Isn't it that a function call cannot have side effect, while a procedure call can?

Does a call to the function in the above example have a side effect to

  • insert into takes values,

  • set errorMsg = ’Enrollment limit reached for course ’ || s_courseid || ’ section ’ || s_secid,

  • select count(*) into currEnrol?

Why can the function have side effect?

In a SQL standard, such as SQL:1999 (mentioned in the quote) or SQL:2016, what are the answers to the above questions?

Thanks.

1 Answers

Yes, if it is doing an INSERT then that is a side effect.

Related