1

My query runs fine in SQL Developer, but when I copy the same query into the mapper XML file for MyBatis, I get an ORA-00900: invalid SQL statement.

Subset of mapper file:

    <select id="getTotalUniqueUsersByPage">
            resultType="KeyValuePair" 
            parameterType="RequestFilter">
        select KEY as key,sum(single_value) as value from (
            select schcm.title as key
            ,schuc.usage_count AS single_value 
            from usage_count schuc
            inner join content_master schcm
            on schcm.trace_id=schuc.trace_key
            where schcm.pagetype='Page'
         ) 
         GROUP by KEY
         order by value desc
    </select>
### The error occurred while setting parameters
### SQL: resultType="KeyValuePair" parameterType="RequestFilter">         select KEY as key,sum(single_value) as value from (             select schcm.title as key             ,schuc.usage_count AS single_value              from usage_count schuc             inner join content_master schcm             on schcm.trace_id=schuc.trace_key             where schcm.pagetype='Page'                                             )           GROUP by KEY          order by value desc
### Cause: java.sql.SQLSyntaxErrorException: ORA-00900: invalid SQL statement

For the most part, regular SQL that runs in SQL Developer also runs when directly pasted into a MyBatis mapper file. Well, mostly anyway. I did remember to check for a semicolon at the end, but didn't make that mistake this time. What I'd like is for the MyBatis call to work like the SQL Developer call.

Update: Tried

   <select id="getTotalUniqueUsersByPage">
            resultType="KeyValuePair" 
           parameterType="RequestFilter">
       select 'SomeTitle' as "Key", 55 as "Value" from dual
    </select>

but that call fails as well with the same "ORA-00900: invalid SQL statement" In deference to comments below, I surrounded the alias names so that the reserved word "Key" should work, and indeed it works find in SQLDeveloper.

9
  • You need to alias your derived table (the select inside the parens). Basically ... from (select ...) <ALIAS> GROUP by KEY.... I can't imagine that SQL would work anywhere. Commented Sep 3, 2019 at 15:48
  • <select id="getTotalUniqueUsersByPage"> resultType="KeyValuePair" parameterType="RequestFilter"> select KEY as key,sum(single_value) as value from ( (select schcm.title as key ,schuc.usage_count AS single_value from usage_count schuc inner join content_master schcm on schcm.trace_id=schuc.trace_key where schcm.pagetype='Page') myselectalias ) GROUP by KEY order by value desc </select> Commented Sep 3, 2019 at 16:10
  • Which Oracle version? Does it support ANSI joins? ;) Commented Sep 3, 2019 at 16:11
  • @Woodsman, you don't need that extra pair of parens. Commented Sep 3, 2019 at 16:13
  • key is a reserved keyword. You need to quote it select "KEY" as "key" ... Commented Sep 3, 2019 at 16:18

1 Answer 1

1

So the problem was not the SQL. The problem was the extra closing > tag in the XML.

 <select id="getTotalUniqueUsersByPage">
            resultType="KeyValuePair" 
           parameterType="RequestFilter">
       select 'SomeTitle' as "Key", 55 as "Value" from dual
    </select>

should be

 <select id="getTotalUniqueUsersByPage"
            resultType="KeyValuePair" 
           parameterType="RequestFilter">
       select 'SomeTitle' as "Key", 55 as "Value" from dual
    </select>

To me, this is an XML validation error, but I'm not sure why it did not show up as such. I'm surprised it made it all the way to Oracle instead of complaining about bad XML instead.

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.