<p>I have 150+ SQL queries in separate text files that I need to analyze (just the actual SQL code, not the data results) in order to identify all column names and table names used. Preferably with the number of times each column and table makes an appearance. Writing a brand new SQL parsing program is trickier than is seems, with nested SELECT statements and the like. </p>

<p>There has to be a program, or code out there that does this (or something close to this), but I have not found it.</p>

[{'answer_id': 56686, 'author': 'joev', 'author_id': 3449, 'author_profile': 'https://Stackoverflow.com/users/3449', 'pm_score': 2, 'selected': False, 'text': '<p>You may want to looking to something like this:</p>\n\n<p><a href="http://jsqlparser.sourceforge.net" rel="nofollow noreferrer" title="JSqlParser">JSqlParser</a></p>\n\n<p>which uses JavaCC to parse and return the query string as an object graph.  I\'ve never used it, so I can\'t vouch for its quality.</p>\n'}, {'answer_id': 56888, 'author': 'Craig Tyler', 'author_id': 5408, 'author_profile': 'https://Stackoverflow.com/users/5408', 'pm_score': 2, 'selected': False, 'text': '<p>How about using the Execution Plan report in MS SQLServer? You can save this to an xml file which can then be parsed.</p>\n'}, {'answer_id': 309912, 'author': 'Neil Barnwell', 'author_id': 26414, 'author_profile': 'https://Stackoverflow.com/users/26414', 'pm_score': 2, 'selected': False, 'text': "<p>If you're application needs to do it, and has access to a database that has the tables etc, you could run something like:</p>\n\n<pre><code>SELECT TOP 0 * FROM MY_TABLE\n</code></pre>\n\n<p>Using ADO.NET.  This would give you a DataTable instance for which you could query the columns and their attributes.</p>\n"}, {'answer_id': 2164298, 'author': 'Jason Butler', 'author_id': 5854, 'author_profile': 'https://Stackoverflow.com/users/5854', 'pm_score': 5, 'selected': True, 'text': '<p>I actually ended up using a tool called \n<a href="http://www.dpriver.com/pp/sqlformat.htm" rel="noreferrer">SQL Pretty Printer</a>. You can purchase a desktop version, but I just used the free online application. Just copy the query into the text box, set the Output to "List DB Object" and click the Format SQL button. </p>\n\n<p>It work great using around 150 different (and complex) SQL queries.</p>\n'}, {'answer_id': 13534708, 'author': 'jcrshankar', 'author_id': 957359, 'author_profile': 'https://Stackoverflow.com/users/957359', 'pm_score': 0, 'selected': False, 'text': '<p>Please go with antlr... Write a grammar n follow the steps..which is given in antlr site..eventually you will get AST(abstract syntax tree). For the given query... we can traverse through this and bring all table ,column which is present in the query..</p>\n'}, {'answer_id': 50530231, 'author': 'Dexygen', 'author_id': 34806, 'author_profile': 'https://Stackoverflow.com/users/34806', 'pm_score': 0, 'selected': False, 'text': '<p>In DB2 you can append your query with something such as the following, but 1 is the minimum you can specify; it will throw an error if you try to specify 0:</p>\n\n<p><code>FETCH FIRST 1 ROW ONLY</code> </p>\n'}]
['https://Stackoverflow.com/questions/56677', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5854/']