Search code examples
sqlms-accessibexpert

Why are sql generators using double parenthesis in where clause?


I worked with different kind of auto generated sql statements like MS Access and Firebird sql. When I used some query builders to generate this sql snippets (Access or IBExpert) they often generate more parenthesis than needed.

I don't think about extra parenthesis around some boolean operations, but take for example the following:

select id, name from table as t
where ((t.id = @id))

When I remove them the query works perfectly fine. But why do they get generated that often?


Solution

  • In this case, there is no difference to the query having or not having brackets.

    I've seen this kind of thing before: The parser just throws them in because it does no harm but makes the parsing code a lot simpler. When rendering a node in an AST, wrap it in brackets - simple.

    Otherwise you may have to backtrack to correctly parenthesise OR conditions for example:

    WHERE ((A OR B) AND (C OR D)) // correct
    

    vs

    WHERE A OR B AND C OR D // incorrect