UPSERT查询提供ORA缺失表达式异常

时间:2012-05-28 00:11:14

标签: java sql oracle upsert

我正在尝试使用以下查询更新并插入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)

2 个答案:

答案 0 :(得分:2)

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

删除逗号。

答案 1 :(得分:-1)

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

WHEN NOT MATCHED THEN
INSERT (col1, col2 ... )
VALUES (val1, val2 ... )