1

Oracle 给我错误“ORA-06502: PL/SQL: numeric or value error: character to number conversion error ORA-06512: at line 15 06502. 00000 - “PL/SQL: numeric or value error%s”

DECLARE
idnumber NUMBER(10);
initials VARCHAR2(100);
userid VARCHAR2(100);
old_initials VARCHAR2(100);
CURSOR cursor1 IS
SELECT  s_first, s_last FROM tst_student ORDER BY s_first;
student cursor1%ROWTYPE;
BEGIN
FOR student IN cursor1 LOOP
idnumber :=1;
old_initials := SUBSTR(student.s_first,1,1) || SUBSTR(student.s_last,1,1);
initials := SUBSTR(student.s_first,1,1) || SUBSTR(student.s_last,1,1);
IF initials = old_initials THEN
userid := old_initials || '00' || idnumber + 1;
ELSE idnumber := 1;
END IF;
DBMS_OUTPUT.PUT_LINE(userid);
END LOOP;
END;

它抱怨的那一行是 userid := old_initials || '00' || 身份证号码 + 1;

谢谢

4

2 回答 2

0

将该行更改为:

userid := old_initials || '00' || (idnumber + 1); -- use brackets around the calculation


问题是 Oracle 正在尝试将连接文本添加到数字1中,即 Oracle 将您的原始行解析为userid := (old_initials || '00' || idnumber) + 1

于 2012-11-19T02:21:22.327 回答
0

假设user_id是数字字段,用于to_number将字符串转换为数字:

 userid := to_number(old_initials || '00' || (idnumber + 1));

如果是varchar类型,

 userid := old_initials || '00' || (idnumber + 1);

编辑:要增加,对于 NUMBER:

  idnumber := idnumber +1;
  userid := to_number(old_initials || '00' ||idnumber );

对于 VARCHAR:

 idnumber := idnumber +1;
 userid := old_initials || '00' || idnumber + 1;

EDIT1:更正段:

  idnumber :=1;
  old_initials := '';
  FOR student IN cursor1 LOOP
    initials := SUBSTR(student.s_first,1,1) || SUBSTR(student.s_last,1,1);
    IF initials = old_initials THEN
      idnumber := idnumber +1;
      userid := old_initials || '00' || idnumber;
     ELSE 
      idnumber := 1; 
      old_initials := initials;
    END IF;
    DBMS_OUTPUT.PUT_LINE(userid);
   END LOOP;
  END;
于 2012-11-19T02:24:59.453 回答