-2

I have a SQL WHERE clause which is two pages long. I appear to be missing a bracket. Is there a way in SQL to count the right brackets and left brackets and even determine where the problem is?

The answer may be no, but I am interested to know if it is, meaning I do not have to go through hundreds of lines of code one line at a time.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
James Khan
  • 773
  • 2
  • 18
  • 46
  • 1
    try an online sql formatter: http://sqlformat.org/ this is one of many, it'll format your sql and you might be able to tell by eye where the indentation starts to go wrong. – Tanner Jun 24 '14 at 14:39
  • drop your entire code block into Word or some other text editor and do a count on the bracket character. – durbnpoisn Jun 24 '14 at 14:40
  • 2
    +1 because I cannot understand the downvotes. The question is clearly stated and on-topic. – Heinzi Jun 24 '14 at 14:42
  • 3
    It's because all the answers and comments are for tool recommendations, and tool recommendations are off-topic. Just use proper indentation. – LittleBobbyTables - Au Revoir Jun 24 '14 at 14:44
  • LittleBobbyTables You're right!! This question is a cry for tool recommendation and is only being upvoted by people who love fishing out such recommendations!! – Satwik Nadkarny Jun 24 '14 at 14:54
  • @LittleBobbyTables: It appears that the OP does not know that Pretty Printers even exist, so a tool recommendation might only be the second step. – Heinzi Jun 24 '14 at 14:54
  • @LittleBobbyTables: Can't all questions here be answered by "just do the right thing"? – Ben Thul Jun 24 '14 at 16:12

2 Answers2

2

You can use an SQL pretty printer to automatically format your SQL statement. This should help you find unbalanced parenthesis.

There are many tools available for this purpose, here's an online service that I frequently use:

Various other tools with SQL Server Management Studio integration can be found in this SO question:

Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519
-2

SQL will tell you when you parse or run the query what line the problem is on!

sz1
  • 72
  • 1
  • 8
  • 1
    not really, it'll tell you that it expects a bracket, but it will likely point to the end of the query in question and say "Incorrect syntax near ')'", which doesn't point to where the bracket should be – Tanner Jun 24 '14 at 15:24
  • *"SQL"*? Do you mean *"SQL Server"*? – Peter Mortensen Jan 03 '19 at 17:43