I have a random .sql file with long sql query. I need to runtime get all table names from the insert sql statement to put into an array. I am able to get the table names (Inventory.tableA) out if the insert statement is in (one line) like the format below:
...
Insert into Inventory.tableA;
...
However, if the Insert statement is in multiple lines like below e.g.
Insert into
Inventory.tableA;
OR
Insert into
(blank line)
(blank line)
Inventory.tableA;
Then my query to retrieve the table name will fail. Can you advise how i can get the table name out from the long sql query which the insert statement can be in either one line or multiple lines? What is the best approach here?
Below is the c# query i tried which can handle for 1 line.
public List<string> GetAllTablesNames(string sp)
{
List<string> output = new List<string>();
string[] contentSplit = sp.Split(new string[] { "INSERT INTO " }, StringSplitOptions.None);
for (int a = 1; a < contentSplit.Length; a++)
{
string[] sa_tableName = contentSplit[a].Substring(0, contentSplit[a].IndexOf("\r")).Trim().Split('.');
output.Add(sa_tableName[0] + "." + sa_tableName[1]);
}
return output.Distinct().ToList();
}
Use
singlelinemode