简体   繁体   中英

UPSERT Query gives ORA Missing Expression Exception

I am trying to update and insert into Oracle Database by using this below query. But when I was trying to insert into oracle database by using this below query, I always get the exception as-

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

This is the below UPSERT Query- Is there anything wrong with this below query?

// 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)";

And I am trying to insert like this-

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();

And when I tried to print the query- I got like this on the console-

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)
 "                ? AS SITEID, "+ 
 "                FROM dual ) maybe "+

remove comma.

You still need to list the columns for your insert statement

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM