2012-11-19 58 views
1

Oracle給我的錯誤「ORA-06502:PL/SQL:數字或值錯誤:字符到數字轉換錯誤ORA-06512:在行15 06502. 00000 - 」PL/SQL:數字或值錯誤%的」PL/SQL - 數值或值錯誤

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; 

它抱怨該生產線是用戶ID:= old_initials || '00' ||的IDNumber + 1;

由於

回答

0

變化即line to this:

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


問題是Oracle正在試圖級聯文本添加到數1,即甲骨文解析爲userid := (old_initials || '00' || idnumber) + 1

+0

謝謝,這個固定我的錯誤,但它仍然沒有增加我的價值。我試圖讓它爲用戶分配用戶名。它只是給每個人分配001。 – user1834591

0

假設user_id是數字字段你原來的路線,使用to_number向數字作爲字符串轉換:

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

如果是varchar類型,

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

編輯:要增加,對號碼:

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; 
+0

@ user1834591您不會在任何地方更改變量'idnumber'的值。請參考最新的答案。 –

+0

感謝您的幫助Yogendra。由於某些原因,它仍然沒有增加idnumber:= idnumber + 1;也許我的其他代碼不正確? – user1834591

+0

@ user1834591:您在每次迭代中將'idnumber'賦值爲'1'來指示循環。該語句應該移出FOR循環。請參考最新的答案。 –