5

Below I am creating table.

    public static final String CREATE_SQL = "CREATE TABLE " +DATABASE_TABLE +
        "(ID number(10,0), " +
        " CGUID VARCHAR(255), " + 
        " PGUID VARCHAR(255), " + 
        " SGUID VARCHAR(255), " + 
        " USERID VARCHAR(255), " +
        " ULOC VARCHAR(255), " +
        " SLOC VARCHAR(255), " +
        " PLOC VARCHAR(255), " +
        " ALOC VARCHAR(255), " +
        " SITEID VARCHAR(255), " +
        " ATTRIBUTEID VARCHAR(255), " +
        " ATTRIBUTEVALUE VARCHAR(255), " +
        " PRIMARY KEY ( ID ))";

This is the below UPSERT_SQL query when I am trying to update my database table I am always getting- java.sql.SQLException: ORA-00936: missing expression . I checked my SQL, I am not able to find where the expression is missing. Is something wrong with the below SQL?

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, "+
    "                ? AS ATTRIBUTEID, "+
    "                ? AS ATTRIBUTEVALUE, "+
    "                FROM dual ) maybe "+
    "   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 "+
    "         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, maybe.ATTRIBUTEID, maybe.ATTRIBUTEVALUE)";

And from below I am executing that UPSERT_SQL Statement.

LnPDataConstants.PSTMT = LnPDataConstants.DB_CONNECTION.prepareStatement(LnPDataConstants.UPSERT_SQL); // create a statement
                        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.setString(11, "10200");
                        LnPDataConstants.PSTMT.setString(12, attrValue1.toString().split("=")[1]);
                        LnPDataConstants.PSTMT.executeUpdate();
3
  • 2
    Is the comma before FROM DUAL ignored by the sql parser? Commented May 9, 2012 at 17:16
  • As Luciano stated, the most likely cause is the comma after "? as attributevalue" because the parser would be expecting another expression after the comma. Commented May 9, 2012 at 17:21
  • @Luciano you should make a post so the question can be marked as resolved :) Commented May 9, 2012 at 17:22

1 Answer 1

16

Yes, there is something wrong with the SQL, and it is that you wrote a comma before FROM dual. This causes Oracle's SQL parser to complain.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.