【问题标题】:How to compare Integer type record with String Type Record in MS_Access?如何比较 MS_Access 中的整数类型记录和字符串类型记录?
【发布时间】:2015-03-27 09:57:50
【问题描述】:

我的数据库中有两个表。 表Other 具有RMA_no'Number' & 表 Material_Require_Master 具有 RMA_NoTEXT

我想执行一个可以输出匹配RMA_No的元组的查询?

到目前为止,我已经做到了。

String sql="Select RMA_Master.RMA_No,Call_Date,Source,Item_name,
                   Booking_Desc,Customer_name,Customer_contact,
                   AssignedTo,Part_No,Part_Name,Part_Quantity,
                   Part_Price,Total 
            From RMA_Master,Material_Require_Master 
            WHERE RMA_Master.RMA_No=Material_Require_Master.RMA_No AND
                  RMA_Master.MaterialRequireStatus='"+materialStatus+"' 
            UNION 
            Select Other.RMA_No,Call_Date,Source,Item_name,
                   Booking_Desc,Customer_name,Customer_contact,
                   AssignedTo,Part_No,Part_Name,Part_Quantity,
                   Part_Price,Total 
            From Other,Material_Require_Master 
            WHERE String.valueOf(Other.RMA_No)= Material_Require_Master.RMA_No AND
                  Other.MaterialRequireStatus='"+materialStatus+"'";

PreparedStatement pst=con.prepareStatement(sql);
ResultSet rs=pst.executeQuery();    
table.setModel(DbUtils.resultSetToTableModel(rs));          
rs.close();
pst.close();

我的 StackTrace 是:

Exception: [Microsoft][ODBC Microsoft Access Driver] Undefined function 'String.valueOf' in expression.

还有当我不使用String.valueOf:

String sql="Select RMA_Master.RMA_No,Call_Date,Source,Item_name,Booking_Desc,Customer_name,Customer_contact,AssignedTo,Part_No,Part_Name,Part_Quantity,Part_Price,Total From RMA_Master,Material_Require_Master WHERE RMA_Master.RMA_No=Material_Require_Master.RMA_No AND RMA_Master.MaterialRequireStatus='"+materialStatus+"' UNION Select Other.RMA_No,Call_Date,Source,Item_name,Booking_Desc,Customer_name,Customer_contact,AssignedTo,Part_No,Part_Name,Part_Quantity,Part_Price,Total From Other,Material_Require_Master WHERE Other.RMA_No= Material_Require_Master.RMA_No AND Other.MaterialRequireStatus='"+materialStatus+"'";

出现以下Exception

Exception: [Microsoft][ODBC Microsoft Access Driver] Type mismatch in expression.

【问题讨论】:

    标签: java database ms-access jdbc odbc


    【解决方案1】:

    我认为您在 SQL 和 Java 之间搞混了。 String.valueOf() 是您不能在 SQL 语句中使用的 Java 方法。 SQL 中有STR(),可用于将数字转换为字符串。将其与LTRIM() 结合使用以删除空白填充,例如LTRIM(STR(Other.RMA_no)).

    但无论如何,正如@Gord 所建议的那样,反过来会更好。

    【讨论】:

      【解决方案2】:

      有两个测试表

      [Material_Require_Master]
      RMA_No - 文本
      mrmText - 文本

      [其他]
      RMA_no - 数字(长整数)
      otherText - 文本

      以下查询...

      SELECT Other.RMA_no, Other.otherText, Material_Require_Master.mrmText
      FROM Material_Require_Master INNER JOIN Other 
          ON Material_Require_Master.RMA_No = Other.RMA_no;
      

      ...确实会产生错误

      表达式中的类型不匹配。

      如果我们在join的ON子句中加入CLng()函数,将Material_Require_Master.RMA_No从文本转换为长整数...

      SELECT Other.RMA_no, Other.otherText, Material_Require_Master.mrmText
      FROM Material_Require_Master INNER JOIN Other 
          ON CLng(Material_Require_Master.RMA_No) = Other.RMA_no;
      

      ...然后查询运行没有错误。

      【讨论】:

      • 不成功..它产生相同的错误。
      • 我用过:SELECT Other.RMA_No,Material_Require_Master.VAT FROM Material_Require_Master INNER JOIN Other ON CLng(Material_Require_Master.RMA_No) = Other.RMA_No
      • ... 这会给您“表达式中的类型不匹配”。也有错误?如果是这样,请尝试将 CLng() 也应用于 Other.RMA_No 看看是否有帮助。
      • 没有用。还是一样的错误
      • 您可以通过以下方式获得成功: Val(Nz((Material_Require_Master.RMA_No)) = Other.RMA_no; ... 或: Nz(Material_Require_Master.RMA_No) = Str(Other.RMA_no); 。 .. 如果是这样,请尝试省略 Nz,它可能不需要。
      猜你喜欢
      • 2013-12-06
      • 2021-12-26
      • 1970-01-01
      • 1970-01-01
      • 2022-08-10
      • 2021-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多