【发布时间】:2013-08-27 15:46:23
【问题描述】:
在下面的代码中,我比较了表 S_R_VAL 和 R_VAL 的两个结果集。我需要比较 S_NUMBER(来自 S_R_VAL 表)和 S_NO(来自 R_VAL 表)。 比较时我收到此错误 "java.lang.RuntimeException: The two ResultSets contains different number of columns! 。请帮助解决此错误。
String SSQ = "select DISTINCT S_NUMBER from OTG.S_R_VAL" +
" WHERE R_TS = (SELECT MAX(R_TS) FROM OTG.S_R_VAL) order by S_NUMBER";
String SDS = "SELECT DISTINCT S_NUMBER FROM OTG.S_R_VAL AS STG WHERE S_NUMBER NOT IN" +
"(SELECT S_NO FROM OTG.R_VAL AS REV WHERE STG.S_NUMBER = REV.S_NO )";
String SSR = "SELECT DISTINCT S_NO FROM OTG.R_VAL where S_NO != 'NULL' order by S_NO";
String SSO = "Select O_UID from OTG.OPTY where C_S_NO IN" +
"( SELECT DISTINCT S_NUMBER FROM OTG.S_R_VAL AS STG WHERE S_NUMBER NOT IN(SELECT S_NO FROM OTG.R_VAL AS REV WHERE STG.S_NUMBER = REV.S_NO ))";
//Statement statement;
try {
connection = DatabaseConnection.getCon();
statement = connection.createStatement();
statement1 = connection.createStatement();
ResultSet SQ = statement.executeQuery(SSQ);
ResultSet DS = statement.executeQuery(SDS);
ResultSet SR = statement.executeQuery(SSR);
ResultSet SO = statement.executeQuery(SSO);
while (rs.next() && SR.next(){
String res1 = rs.getString("S_NUMBER");
String res2 = SR.getString("S_NO");
StringBuffer updateQuery = new StringBuffer();
if (res1.equals(res2)) {
throw new RuntimeException(String.format("%s and %s aren't equal at common position %d",
res1, res2));
}else
{
throw new RuntimeException("The two ResultSets contains different number of columns!");
}
}
connection.commit();
【问题讨论】:
-
你的代码看起来很奇怪。您将 5 列 (
SSQ) 的结果与 1 列 (SSR) 的结果进行比较 - 您只是将S_NUMBER与S_NO进行比较,这可以使用单个 SQL 查询更有效地完成(例如带有EXCEPT运算符)。逐行执行此操作,尤其是没有ORDER BY很可能无法正常工作。 -
嗨,是的,你说得对。我将 S_NUMBER 与 S_NO 进行比较。你能帮助如何以正确的方式解决这些问题吗?这是导致错误..当,比较。