2016-08-17 54 views
0

這可能是非常基本的。我是PL/SQL的初學者,但我堅持這個問題。如果有人知道解決方案,請告訴我。PL/SQL如何賦值變量?

此代碼

DECLARE  
    v_objectID VARCHAR2(100); 
    v_account VARCHAR2(100); 

BEGIN   
    v_objectID :='21,22'; 

    DBMS_OUTPUT.PUT_LINE(v_objectID); 
END; 
/

輸出是

21,22

然後,

此代碼

DECLARE  
    v_objectID VARCHAR2(100); 
    v_account VARCHAR2(100); 

BEGIN   



    SELECT LISTAGG(x.ACCOUNT, ',') WITHIN GROUP (ORDER BY NULL) AS ACCOUNT 
    INTO v_account   
    FROM acctx x 
    where x.OBJECT_ID IN (21,22); 

    DBMS_OUTPUT.PUT_LINE(v_account); 
END; 
/

OUTPUT是

1001,2002

然後我嘗試做這樣的

DECLARE  
    v_objectID VARCHAR2(100); 
    v_account VARCHAR2(100); 

BEGIN   
    v_objectID :='21,22'; 


    SELECT LISTAGG(x.ACCOUNT, ',') WITHIN GROUP (ORDER BY NULL) AS ACCOUNT 
    INTO v_account   
    FROM acctx x 
    where x.OBJECT_ID IN (v_objectID); 

    DBMS_OUTPUT.PUT_LINE(v_account); 
END; 
/

我加v_objectID:= '21,22' ;;這是造成問題

的錯誤是

ORA_07122:無效的號碼 ORA-06512:在第9行

我應該如何分配變量適當地輸出1001,2002?

感謝

回答

1

的錯誤是顯而易見的,在你的表的object_id本來NUMBER數據類型的。現在你正在試圖比較一個數字與一個varchar,所以你面臨的問題。請嘗試以下:

DECLARE  
    v_objectID VARCHAR2(100); 
    v_account VARCHAR2(100); 

BEGIN   
    v_objectID :='21,22'; 


    SELECT LISTAGG(x.A, ',') WITHIN GROUP (ORDER BY NULL) AS ACCOUNT 
    INTO v_account   
    FROM test x 
    where to_char(x.A) IN (v_objectID); 

    DBMS_OUTPUT.PUT_LINE(v_account); 
END; 
/