views:

433

answers:

3

Here is my query:

SELECT * FROM Auta WHERE SUBSTR(spz, 1, 2) = 
(SELECT SUBSTR(spz, 1, 2) FROM Auta WHERE typ = 'BMW' AND specifikacia_typu = 'Z1' LIMIT 1);

And when I run it I get this error:

ORA-00907: missing right parenthesis

I'm getting a little desperate, I've already tried adding parentheses everywhere in the query and I still get the error? There are 3 left and 3 right parentheses in the query so everything should be all right.

+6  A: 

Hi Richard,

The LIMIT clause doesn't exist in Oracle. Instead you would use rownum:

SELECT *
  FROM Auta
 WHERE SUBSTR(spz, 1, 2) = (SELECT SUBSTR(spz, 1, 2)
                              FROM Auta
                             WHERE typ = 'BMW'
                               AND specifikacia_typu = 'Z1'
                               AND ROWNUM = 1);
Vincent Malgrat
Thank you. I'm new to Oracle, I'm used to MySQL and PostgreSQL :)
Richard Knop
You need to be careful with the "ROWNUM = 1" syntax. I don't know if it'll work in this scenario or not, but the record that's returned from the subquery is indeterminate - i.e. you might get different records returned each time you run the query. If you want a consistent return, wrap the subquery in another subquery with an "order by".
Nick Pierpoint
@Nick: the result of the original LIMIT 1 would also be undeterminate. There is no ORDER BY anyway so I assumed any row would be ok.
Vincent Malgrat
+3  A: 

What's that "LIMIT 1" for? I don't believe that's correct Oracle syntax. If you're trying to limit the output to one row, use:

WHERE rownum = 1

DCookie
+3  A: 

LIMIT 1 is a MySQLism, as far as I know.

  SELECT *
    FROM Auta
   WHERE SUBSTR(spz, 1, 2) =
         (SELECT SUBSTR(spz, 1, 2)
          FROM Auta
         WHERE typ = 'BMW'
           AND specifikacia_typu = 'Z1'
           AND ROWNUM = 1);

...should give you the expected results

Rob