0

我正在尝试使用以下查询更新并插入 Oracle 数据库。但是,当我尝试使用以下查询插入 oracle 数据库时,我总是得到异常:

java.sql.SQLException: ORA-00936: missing expression

这是下面的 UPSERT 查询 - 下面的查询有什么问题吗?

// Update and Insert both
    public static final String UPSERT_SQL = "MERGE INTO " +DATABASE_TABLE+ " USING (  SELECT ? AS ID, " +    // We will maybe add this record
    "                ? AS CGUID, " +
    "                ? AS PGUID, " +
    "                ? AS SGUID, "+
    "                ? AS USERID, "+
    "                ? AS ULOC, "+
    "                ? AS SLOC, "+
    "                ? AS PLOC, "+
    "                ? AS ALOC, "+
    "                ? AS SITEID, "+
    "                FROM dual ) maybe "+
    // Checking whether ID got matched, if matched then we will update the database table (ULOC, SLOC, PLOC, ALOC)
    "   ON (maybe.ID = "+DATABASE_TABLE+".ID) "+
    "         WHEN MATCHED THEN "+
            // We only need update the fields that might have changed
    "       UPDATE SET " +DATABASE_TABLE+ ".ULOC = maybe.ULOC, " +DATABASE_TABLE+ ".SLOC = maybe.SLOC, " +DATABASE_TABLE+ ".PLOC = maybe.PLOC, " +DATABASE_TABLE+ ".ALOC = maybe.ALOC "+
    // If not matched then we will Insert new records in the database.
    "         WHEN NOT MATCHED THEN "+
       // Insert new record
    "   INSERT VALUES (maybe.ID, maybe.CGUID, maybe.PGUID, maybe.SGUID, maybe.USERID, maybe.ULOC, maybe.SLOC, maybe.PLOC, maybe.ALOC, maybe.SITEID)";

我正在尝试像这样插入-

LnPDataConstants.PSTMT = LnPDataConstants.DB_CONNECTION.prepareStatement(LnPDataConstants.UPSERT_SQL);
LnPDataConstants.PSTMT.setInt(1, (int) ind);
LnPDataConstants.PSTMT.setString(2, LnPDataConstants.CGUID_VALUE);
LnPDataConstants.PSTMT.setString(3, LnPDataConstants.PGUID_VALUE);
LnPDataConstants.PSTMT.setString(4, LnPDataConstants.SGUID_VALUE);
LnPDataConstants.PSTMT.setString(5, LnPDataConstants.UID_VALUE);
LnPDataConstants.PSTMT.setString(6, LnPDataConstants.ULOC_VALUE);
LnPDataConstants.PSTMT.setString(7, LnPDataConstants.SLOC_VALUE);
LnPDataConstants.PSTMT.setString(8, LnPDataConstants.PLOC_VALUE);
LnPDataConstants.PSTMT.setString(9, LnPDataConstants.ALOC_VALUE);
LnPDataConstants.PSTMT.setString(10, LnPDataConstants.SITEID_VALUE);
LnPDataConstants.PSTMT.executeUpdate();

当我试图打印查询时——我在控制台上得到了这样的结果——

MERGE INTO LNPDATA USING (  SELECT ? AS ID,                 ? AS CGUID,                 ? AS PGUID,                 ? AS SGUID,                 ? AS USERID,                 ? AS ULOC,                 ? AS SLOC,                 ? AS PLOC,                 ? AS ALOC,                 ? AS SITEID,                 FROM dual ) maybe    ON (maybe.ID = LNPDATA.ID)          WHEN MATCHED THEN        UPDATE SET LNPDATA.ULOC = maybe.ULOC, LNPDATA.SLOC = maybe.SLOC, LNPDATA.PLOC = maybe.PLOC, LNPDATA.ALOC = maybe.ALOC          WHEN NOT MATCHED THEN    INSERT VALUES (maybe.ID, maybe.CGUID, maybe.PGUID, maybe.SGUID, maybe.USERID, maybe.ULOC, maybe.SLOC, maybe.PLOC, maybe.ALOC, maybe.SITEID)
4

2 回答 2

2
 "                ? AS SITEID, "+ 
 "                FROM dual ) maybe "+

去掉逗号。

于 2012-05-28T00:41:13.370 回答
-1

您仍然需要列出插入语句的列

WHEN NOT MATCHED THEN
INSERT (col1, col2 ... )
VALUES (val1, val2 ... )
于 2012-05-28T00:54:12.117 回答