无效数字异常在从Java调用select语句时在SQL中引发。

huangapple go评论88阅读模式
英文:

Invalid Number Exception in SQL when trying to call select statement from Java

问题

我正在尝试从我的Java程序中调用一个select查询,但是我得到了无效数字的错误。

我正在尝试像这样调用查询:

code

 String[] l_arr = new String[]{"CUSTOMERCUMULATIVELIMIT"};

    l_rs = JDBCEngine.executeQuery(MessageFormat.format(
    					" SELECT  LIMITS,  P.PACKAGE_ID  FROM MSTGLOBALLIMITSPACKAGE P  ,MSTCUSTOMERPROFILE C  WHERE P.PACKAGE_ID = {0}   AND C.ID_CUST = ?    AND C.ID_ENTITY = ?  AND C.TYPEUSER = ?  AND C.ID_ENTITY = P.ID_ENTITY  AND P.PACKAGE_ID NOT LIKE ?",l_arr );

我得到的查询如下:

Result

SELECT LIMITS, P.PACKAGE_ID
  FROM MSTGLOBALLIMITSPACKAGE P, MSTCUSTOMERPROFILE C
 WHERE P.PACKAGE_ID = CUSTOMERCUMULATIVELIMIT
   AND C.ID_CUST = '00000144'
   AND C.ID_ENTITY = 'B001'
   AND C.TYPEUSER = 'ECU'
   AND C.ID_ENTITY = P.ID_ENTITY
   AND P.PACKAGE_ID NOT LIKE '*%'

我在这一行P.PACKAGE_ID = {0}中得到了无效数字错误。我经过了研究,了解到如果我使用to_char,我可以避免这个错误。但我不确定如何在P.PACKAGE_ID = {0}中使用to_char

另外,PACKAGE_ID的类型是Varchar2,而CUSTOMERCUMULATIVELIMITMSTCUSTOMERPROFILE中类型为Number的列。

任何帮助或建议将不胜感激。谢谢。

英文:

I am trying to call a select query from my Java Program but i am getting Invalid Numbererror.
I am trying to call the query something like this

code

 String[] l_arr = new String[]{"CUSTOMERCUMULATIVELIMIT"};

    l_rs = JDBCEngine.executeQuery(MessageFormat.format(
    					" SELECT  LIMITS,  P.PACKAGE_ID  FROM MSTGLOBALLIMITSPACKAGE P  ,MSTCUSTOMERPROFILE C  WHERE P.PACKAGE_ID = {0}   AND C.ID_CUST = ?    AND C.ID_ENTITY = ?  AND C.TYPEUSER = ?  AND C.ID_ENTITY = P.ID_ENTITY  AND P.PACKAGE_ID NOT LIKE ?",l_arr );

I am getting query something like this:

Result

SELECT LIMITS, P.PACKAGE_ID
  FROM MSTGLOBALLIMITSPACKAGE P, MSTCUSTOMERPROFILE C
 WHERE P.PACKAGE_ID = CUSTOMERCUMULATIVELIMIT
   AND C.ID_CUST = '00000144'
   AND C.ID_ENTITY = 'B001'
   AND C.TYPEUSER = 'ECU'
   AND C.ID_ENTITY = P.ID_ENTITY
   AND P.PACKAGE_ID NOT LIKE '*%'

I am getting the error Invalid number in the line P.PACKAGE_ID = {0}. I gone through and come to know that if i use to_char i can avoid this error. But I am not sure how to use to_char in P.PACKAGE_ID = {0}.

Also PACKAGE_ID is of type Varchar2 and CUSTOMERCUMULATIVELIMIT is a column in MSTCUSTOMERPROFILE of type Number.

Any help or suggestion will be appreciated. Thanks.

答案1

得分: 1

只需在你的SQL中添加 to_char,将 {0} 更改为 to_char({0})

l_rs = JDBCEngine.executeQuery(MessageFormat.format("SELECT LIMITS, P.PACKAGE_ID FROM MSTGLOBALLIMITSPACKAGE P, MSTCUSTOMERPROFILE C WHERE P.PACKAGE_ID = to_char({0}) AND C.ID_CUST = ? AND C.ID_ENTITY = ? AND C.TYPEUSER = ? AND C.ID_ENTITY = P.ID_ENTITY AND P.PACKAGE_ID NOT LIKE ?", l_arr););
英文:

just add to_char to your sql, change {0} to to_char({0})

l_rs = JDBCEngine.executeQuery(MessageFormat.format( " SELECT LIMITS, P.PACKAGE_ID FROM MSTGLOBALLIMITSPACKAGE P ,MSTCUSTOMERPROFILE C WHERE P.PACKAGE_ID = to_char({0}) AND C.ID_CUST = ? AND C.ID_ENTITY = ? AND C.TYPEUSER = ? AND C.ID_ENTITY = P.ID_ENTITY AND P.PACKAGE_ID NOT LIKE ?",l_arr );

答案2

得分: 0

你正在处理以下代码行:

WHERE P.PACKAGE_ID = CUSTOMERCUMULATIVELIMIT

由于没有单引号,SQL将CUSTOMERCUMULATIVELIMIT视为数字,而不是字符串。

添加单引号应该可以解决这个问题。

l_rs = JDBCEngine.executeQuery(MessageFormat.format(
                        " SELECT  LIMITS,  P.PACKAGE_ID  FROM MSTGLOBALLIMITSPACKAGE P  ,MSTCUSTOMERPROFILE C  WHERE P.PACKAGE_ID = '{0}'   AND C.ID_CUST = ?    AND C.ID_ENTITY = ?  AND C.TYPEUSER = ?  AND C.ID_ENTITY = P.ID_ENTITY  AND P.PACKAGE_ID NOT LIKE ?", l_arr );
英文:

You are working with this line :

WHERE P.PACKAGE_ID = CUSTOMERCUMULATIVELIMIT

As there are no single quotes, SQL consider CUSTOMERCUMULATIVELIMIT as a number, not a string.

Adding quotes should solve this.

l_rs = JDBCEngine.executeQuery(MessageFormat.format(
                        " SELECT  LIMITS,  P.PACKAGE_ID  FROM MSTGLOBALLIMITSPACKAGE P  ,MSTCUSTOMERPROFILE C  WHERE P.PACKAGE_ID = '{0}'   AND C.ID_CUST = ?    AND C.ID_ENTITY = ?  AND C.TYPEUSER = ?  AND C.ID_ENTITY = P.ID_ENTITY  AND P.PACKAGE_ID NOT LIKE ?",l_arr );

huangapple
  • 本文由 发表于 2020年8月24日 13:49:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63555371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定