PL / SQL - 数字或值错误

时间:2012-11-19 02:17:13

标签: sql oracle plsql

Oracle给出了错误“ORA-06502:PL / SQL:数字或值错误:字符到数字转换错误ORA-06512:在第15行 06502. 00000 - “PL / SQL:数字或值错误%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'|| idnumber + 1;

由于

2 个答案:

答案 0 :(得分:0)

将该行更改为:

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


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

答案 1 :(得分: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;