SQL - Missing right parenthesis error - mysql-error-1064

SQL Code
SELECT DtsDavaIslem.DAVAISLEM_NO, DtsDavaIslem.DOSYA_ID, DtsDavaIslem.ISEMRI_NO,
DtsDavaIslem.GIREN_KULLANICI, DtsDavaIslem.GIRIS_TARIHI, DtsDavaIslem.DEGISTIREN_KULLANICI,
DtsDavaIslem.DEGISTIRME_TARIHI, DtsDavaIslem.ISLEMLISTESINO, DtsDavaIslem.YAPILACAKIS,
DtsDavaIslem.BASLANGICTARIHI, dtsdavaislem.bitmesigerekentarih, (SELECT
DtsDavaIslemListesi.ISLEM_ADI || ';' || DtsDavaIslem.ISLEMLISTESINO FROM DTS.DAVA_ISLEMLISTESI DtsDavaIslemListesi WHERE
DtsDavaIslemListesi.ISLEMLISTESINO=DtsDavaIslem.ISLEMLISTESINO ) AS islem_auto_suggest FROM DTS.DAVA_ISLEM DtsDavaIslem WHERE 1=1
Do you edit the SQL code for me please?
Error
oracle.jbo.SQLStmtException: JBO-27122: Deyim haz?rl??? s?ras?nda SQL hatas? olu?tu. Deyim: SELECT * FROM (SELECT * FROM (SELECT DtsDavaIslemListesi.ISLEMLISTESINO,
DtsDavaIslemListesi.ISLEM_ADI,
DtsDavaIslemListesi.GIREN_KULLANICI,
DtsDavaIslemListesi.GIRIS_TARIHI,
DtsDavaIslemListesi.DEGISTIREN_KULLANICI,
DtsDavaIslemListesi.DEGISTIRME_TARIHI,
DtsDavaIslemListesi.dava_durumno,
DtsDavaIslemListesi.ISLEM_ADI || ';' || DtsDavaIslemListesi.ISLEMLISTESINO as Aciklama
FROM dts.dava_islemlistesi dtsdavaislemlistesi
WHERE :bSorgu<>'1'
AND 1=1) QRSLT WHERE ( ( (ACIKLAMA = :vc_temp_1 ) ) )
ORA-00907: missing right parenthesis
Do you edit the SQL code for me please?

You have 4 left "(" and 3 right ")". They are called "parenthesis". If the error says missing right parenthesis, it means you're missing one or more ")". Next time first google it (or bing it :P) before asking such a question.

The error isn't in the SQL from beginning but in the sql that your code is making up. So look at the sql before it executes and look where you went wrong.
If you look at the error you can se how the SQL is wrong, but you will need to look at your own code to find where you're building it up wrong.

Related

ORA-00936: missing expression Solution- Convert function

when doing this query in oracle I get the error
ORA-00936: missing expression
00936. 00000 - "missing expression"
If I run the query to the from of it gives me results, then I deduce that the problem comes from where, however, I cannot identify what it is
SELECT FECHADOC, FECHACONT, CLASEDOC, SOCIEDAD, MONEDA, TIPOCAMBIO, PERIODO,
REFERENCIA, TEXTOCAB, ID_REGISTRO
FROM ESQUEMA.TABLE
WHERE CONVERT(CHAR(8),20211231,112) <= CONVERT(CHAR(8),DATEADD(DAY,-90,GETDATE()),112)
I already used:
WHERE CONVERT( TO_CHAR(8),20201231,112) <=
(CONVERT(TO_CHAR(8),DATEADD(DAY,-90,GETDATE()),112) )
and it keeps giving me an error
If this really is Oracle, then dateadd and getdate aren't Oracle functions. Look like MS SQL Server ones. Also, table is reserved word for tables, you can't name a table (or any other object) table.
Anyway: looks like this is what you might be looking for:
SELECT FECHADOC, FECHACONT, CLASEDOC, SOCIEDAD, MONEDA,
TIPOCAMBIO, PERIODO, REFERENCIA,
TEXTOCAB, ID_REGISTRO
FROM ESQUEMA.TABLE
where to_date('20211231', 'yyyymmdd') <= trunc(sysdate) - 90;

How do I use FETCH FIRST 1 ROWS ONLY in combination with UNION ALL in DB2?

See title.
This is what I'm trying:
select a.work_order_no
from (
select work_order_no as work_order_no
from work_order_line
where insert_timestamp is not null
FETCH FIRST 1 ROWS ONLY
union all
select work_order_no as work_order_no
from work_order_line
where insert_timestamp is null
FETCH FIRST 1 ROWS ONLY
) as a
FETCH FIRST 1 ROWS ONLY
But it give the following error:
SQL State: 42601 Vendor Code: -199 Message: [SQL0199] Keyword UNION not expected. Valid tokens: ). Cause . . . . . :   The keyword UNION was not expected here.  A syntax error was detected at keyword UNION.  The partial list of valid tokens is ). This list assumes that the statement is correct up to the unexpected keyword.  The error may be earlier in the statement but the syntax of the statement seems to be valid up to this point. Recovery  . . . :   Examine the SQL statement in the area of the specified keyword.  A colon or SQL delimiter may be missing. SQL requires reserved words to be delimited when they are used as a name. Correct the SQL statement and try the request again.  Processing ended because the highlighted statement did not complete successfully  Failed statements: 1
In SQL this concept would work with the 'top 1' syntax. I'm assuming this can also work in DB2 but I'm just doing something wrong with the syntax order?
I have asked a colleague and luckily he responded rather quickly:
I missed some ()
select a.work_order_no
from (
(select work_order_no as work_order_no
from work_order_line
where insert_timestamp is not null
FETCH FIRST 1 ROWS ONLY)
union all
(select work_order_no as work_order_no
from work_order_line
where insert_timestamp is null
FETCH FIRST 1 ROWS ONLY )
) as a
FETCH FIRST 1 ROWS ONLY

How to find out the right syntax in MariaDB Error Based SQL Injection?

I am trying to inject SQL statements into a Box.
I have the following injection point:
example.com/?o=1&page=app
when I inject 1' then I receive the following error message:
DEBUG INFO: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '5' or dest like '1'') LIMIT 10' at line 1
I was trying to inject the following:
1' ORDER BY 1 --
I still get error message and I don't know how to close the statement:
DEBUG INFO: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ORDER BY 1 --') and ( dest like '5' or dest like '1' ORDER BY 1 --') LIMIT 10' at line 1
What I am doing wrong?
Thanks for the answers!
Given that when you tried 1' the query included '1'' it seems that the original query is something like this:
... '5' or dest like '$o') LIMIT 10
e.g.
SELECT * FROM table WHERE (category = '5' or dest like '$o') LIMIT 10
To get this to be a valid query you'd need to close the parentheses.
So e.g. %') --, giving:
SELECT * FROM table WHERE (category = '5' or dest like '%') --') LIMIT 10
or %' OR '' = ', giving:
SELECT * FROM table WHERE (category = '5' or dest like '%' OR '' = '') LIMIT 10

Missing left parenthesis error

select *
from student3
where marks > (select avg(marks) from table)
missing left parenthesis error occurs when I am running query in Oracle
As Gurvinder (#GurV) firstly pointed out, table is a reserved keyword in Oracle. You can proceed with your current query by escaping table in double quotes, i.e.
select *
from student3
where marks > (select avg(marks) from "table")
But you might want to avoid using table to name your database tables.

oracle SQL and ( ) brackets in where

I have an application which automatically add brackets after WHERE condition and send it to JDBC Oracle driver, Oracle doesn't like it and thrown: ORA-00907: missing right parenthesis
I'm not sure how to work with it in the scope of Oracle syntax, but any suggestion to fix it having this brackets or it's not supported by the syntax?
Original query works just fine:
SELECT count(*) as ErrorCount, Engine_name, to_char(log_time,'hh24') as Hour FROM eailog_data.err_log WHERE err_timestamp > sysdate-1/24 GROUP BY engine_name, to_char(log_time,'hh24') HAVING count(*) > 100 AND count(*) > 0 ORDER BY count(*)
and 3rd party application modify it like this (note brackets added after WHERE condition):
SELECT count(*) as ErrorCount, Engine_name, to_char(log_time,'hh24') as Hour
FROM eailog_data.err_log
WHERE
(
err_timestamp > sysdate-1/24
GROUP BY engine_name,
to_char(log_time,'hh24')
HAVING count(*) > 100
)
AND count(*) > 0
ORDER BY count(*)
Any ideas how to fix SQL with added brackets?
The WHERE clause parenthetical expression needs to end at the end of the WHERE clause and the condition in the HAVING clause ends with a parenthesis, but never begins.
In terms of adding parenthesis, certainly you could add a parenthesis at the end of the WHERE clause and add a parenthesis at the beginning of the HAVING clause as follows:
SELECT count(*) AS errorcount,
engine_name,
to_char(log_time,'hh24') AS HOUR
FROM eailog_data.err_log
WHERE ( err_timestamp > SYSDATE-1/24 )
GROUP BY engine_name,
to_char(log_time,'hh24')
HAVING ( count(*) > 100 )
AND count( *) > 0
ORDER BY count(*)
Since this is an application, it sounds like you need to work with the author of the application to fix their parenthesis usage.
Here is an example using the DUAL table
Before, malformed parenthetical expression in the WHERE and HAVING clause.
SCOTT#dev> SELECT dummy,
2 COUNT(*)
3 FROM dual
4 WHERE (dummy != 'Y'
5 GROUP BY dummy
6 HAVING COUNT( *) = 1)
7 AND COUNT( *) > 0
8 ORDER BY COUNT(*)
9 /
WHERE (dummy != 'Y'
*
ERROR at line 4:
ORA-00907: missing right parenthesis
After, corrected parenthetical expression in the 'WHERE' and 'HAVING' clause.
SCOTT#dev> --corrected
SCOTT#dev> SELECT dummy,
2 COUNT(*)
3 FROM dual
4 WHERE (dummy != 'Y')
5 GROUP BY dummy
6 HAVING (COUNT( *) = 1)
7 AND COUNT( *) > 0
8 ORDER BY COUNT(*)
9 /
D COUNT(*)
= ==========
X 1
A SQL statement consists of several clauses (some of which are optional):
the column list
the table list (FROM clause)
filter conditions (WHERE clause)
aggregate columns (GROUP BY clause)
aggregate conditions (HAVING clause)
etc.
The key concept that seems to be missing is that you can't open a parenthesis in one clause and close it in another. The reason the error you're getting is "missing right parenthesis" is that the SQL engine thinks you're done with the WHERE clause as soon as it sees GROUP BY. Since there was a un-closed parenthetical at that point, it can't parse any further.
To use an analogy, the SQL you provided is like having the opening and closing parenthesis in different methods in Java. It simply can't work.
There are at least two ways to get around some tool mangling your SQL syntax: Creative SQL to subvert the parser, and convert the SQL.
Creative SQL
Parsing Oracle SQL is virtually impossible to do 100% correctly since the syntax is much more complicated than other languages. This leads to problems with beautifiers and code generators. I've seen tools fail in ways very similar to your example.
But this complexity also offers many opportunities to subvert tools that try to rewrite SQL. Try different features until you find something that is immune to their parser. Here are some ideas:
Inline view. select * from (select ... from ... where ... ) where 1=1;
Common table expression. with some_query as (select ... from ... where ... ) select * from some_query;
Alternative quoting mechanism.
select dummy
from dual
where '''' = q'<'>' --The parser probably thinks this is still a string.
and 1 = 1
group by dummy
--' --And it probably thinks this is the end.
Convert SQL
In extreme cases there are ways to make Oracle accept completely broken SQL. Check out
SQL Translation Framework
and DBMS_ADVANCED_REWRITE.
Those tools are definitely a last resort. It would be great if we could wave a magic wand and fix all 3rd party programs but we have to live in the real world.

Resources