Question

I am trying to insert new row in table which is copy of the row selected from the same table. I have created a procedure which selects the row based on id from table and inserts back into the same table with some new values. The procedure also returns PK of the new row.

When I execute CallableStatement, I get below exception:

java.sql.SQLException: ORA-06550: line 1, column 371:
PL/SQL: ORA-00933: SQL command not properly ended
ORA-06550: line 1, column 7:
PL/SQL: SQL Statement ignored

at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)

I am not able to find the error

Commond not properly ended

in my procedure

My Code for the reference. Any help or hint is appreciated. Thank you.

public void updateFileToThisVersion(VaMediaFile fileDTO, Long fromMediaId){

    Connection conn = null;
    CallableStatement cst = null;
    try {
        String insertQuery = "BEGIN INSERT INTO VA_MEDIA_FILE (VMF_ID, VMF_FILE_NAME, VMF_FILE, VMF_FILE_CONTENT_TYPE, VMF_FILE_VER_NUM, VMF_IS_ACTIVE, VMF_UPLOADED_BY_ROLE_ID, VMF_UPLOADED_BY_USER_ID, VMF_UPLOADED_TS, VMF_COMMENTS) " + 
                "  (SELECT VMF_ID_SEQ_NUM.NEXTVAL, VMF_FILE_NAME, VMF_FILE, VMF_FILE_CONTENT_TYPE, ?, 'Y', ?, ?, SYSDATE, VMF_COMMENTS FROM VA_MEDIA_FILE where VMF_ID = ?) RETURNING VMF_ID INTO ?; END;";


        conn = getConnection(); 
        conn.setAutoCommit(false);
        cst = conn.prepareCall(insertQuery);

        cst.setString(1, fileDTO.getFileVerNum());
        cst.setString(2, fileDTO.getUploadedByRoleId());
        cst.setString(3, fileDTO.getUploadedByUserId());
        cst.setLong(4, fromMediaId);            
        cst.registerOutParameter(5, Types.NUMERIC);

        cst.execute();
        long newRecordId = cst.getLong(5);
        if(newRecordId != 0L){                      
            conn.commit();
        }           

    } catch (Exception sqlException) {
    } finally {
        OracleDaoUtils.closeQuietly(cst, conn);     
    }               
}
Was it helpful?

Solution

Oracle doesn't seem to like mixing the "select statement inside the insert statement" with the "returning" clause. First, you will need a separate select statement that selects into a record type that contains all the fields in your select statement. Then you can assign your vmf_id from your record to your JDBC output parameter. Finally you can use the record in an insert statement.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top