PL / SQL如何赋值变量?

时间:2016-08-17 14:53:24

标签: string variables plsql

这可能是非常基本的。我是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 个答案:

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