注意每个if都要用end if结尾,同时end if后要用封号结尾,else if 要写成elsif,select into 后要验证是不是null

create or replace function getMaxID(v_type Varchar2) return varchar2 is
temp varchar2(10);
 num NUMBER;
  BEGIN
IF v_type='U' THEN
  SELECT Substr(MAX(userid), 2, 5)  INTO temp  FROM users;
      IF temp IS NULL THEN
       temp:='Z00001';
        RETURN(temp);
      Else
      num:=to_number(temp)+1;
      temp:='Z'||ltrim(to_char(num,'00000'));
      END IF;
ELSIF  v_TYPE='M' THEN
  SELECT Substr(MAX(mid), 2, 5)  INTO temp  FROM module;
      IF temp IS NULL THEN
      temp:='M00001';
       RETURN(temp);
      END IF;
      num:=to_number(temp)+1;
      temp:='M'||ltrim(to_char(num,'00000'));
ELSIF  v_TYPE='A' THEN
  SELECT Substr(MAX(Aid), 2, 5)  INTO temp  FROM article;
      IF temp IS NULL THEN
      temp:='A00001';
      RETURN(temp);
      END IF;
      num:=to_number(temp)+1;
      temp:='A'||ltrim(to_char(num,'00000'));
ELSIF  v_TYPE='A' THEN
  SELECT Substr(MAX(rid), 2, 5)  INTO temp  FROM reply;
      IF temp IS NULL THEN
      temp:='R00001';
       RETURN(temp);
      END IF;
      num:=to_number(temp)+1;
      temp:='R'||ltrim(to_char(num,'00000'));
END IF;
 
 RETURN(temp);
END;

posted on 2010-01-12 20:26  新小白龙  阅读(289)  评论(0编辑  收藏  举报