Tech Tip: How to Parse an SQL Command?

Hadi Fadlallah
Tech Blog
Published in
2 min readFeb 26, 2023
Photo by Caspar Camille Rubin on Unsplash

It is not easy to extract object names from an SQL command since they may be written in different ways (with/without schema, databases name included …). Still, there are many option to extract objects from an SQL query that you can try.

This quick tip illustrates different approaches to parse a SQL Command.

Regular Expressions

Regular expressions are patterns used to match character combinations in strings. They can be used to extract the different parts composing a SQL command. For example: You have to search for the words located after the following keywords:

  • TRUNCATE TABLE
  • FROM
  • UPDATE
  • JOIN

The following code is a C# example:

Regex regex = new Regex(@"\bJOIN\s+(?<Retrieve>[a-zA-Z\._\d\[\]]+)\b|\bFROM\s+(?<Retrieve>[a-zA-Z\._\d\[\]]+)\b|\bUPDATE\s+(?<Update>[a-zA-Z\._\d]+)\b|\bINSERT\s+(?:\bINTO\b)?\s+(?<Insert>[a-zA-Z\._\d]+)\b|\bTRUNCATE\s+TABLE\s+(?<Delete>[a-zA-Z\._\d]+)\b|\bDELETE\s+(?:\bFROM\b)?\s+(?<Delete>[a-zA-Z\._\d]+)\b");

var obj = regex.Matches(sql);

foreach(Match m in obj)
{

Console.WriteLine(m.ToString().Substring(m.ToString().IndexOf(" ")).Trim());

}

SQL parsers

There are several open-source projects and third-party tools for parsing SQL commands.

.NET Tsql Parser

This is an official .NET library that accurately produces an Abstract Syntax Tree (AST) representation of the T-SQL code.

SQLParser.com

A commercial SQL parser developed by GUDU software company.

GitHub SQL Parsers

There are several SQL parsers published on GitHub. They are listed under the sql-parser topic.

Other projects

Other open-source projects can be found on different public repositories such as Code Project.

--

--

Hadi Fadlallah
Tech Blog

I write about data engineering, data management, SQL, and anything related to data. https://thedataengineer.blog