2016-03-07 53 views
0

我有一個查詢,我需要同 不使用IN操作符備選的IN運算符或從查詢中的函數替換IN運算符?

這裏查詢

create function registerStudent(
     in s_id varchar(5), 
     in s_courseid varchar (8), 
     in s_secid varchar (8), 
     in 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 secid = 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 secid = 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 

      set errorMsg = 'Enrollment limit reached for course ' I I s_courseid II ' section ' I I s_secid; 
      return(-1); 
     end; 
+0

我正在使用MYSQL –

+2

你在哪裏使用了IN運算符? –

+0

NEAR CRAETE FUNCTION –

回答

0

你列出,括號內的存儲功能的所有參數。默認情況下,所有參數都是IN參數。您無法指定參數的IN , OUT or INOUT修飾符。

create function registerStudent(
     s_id varchar(5), 
     s_courseid varchar (8), 
     s_secid varchar (8), 
     semester varchar (6), 
     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 secid = 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 secid = 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 

     set errorMsg = 'Enrollment limit reached for course ' I I s_courseid II ' section ' I I s_secid; 
     return(-1); 
    end;