title: Getting table name from query through c# .net
body: <p>I have been tasked with writing a function which will be passed a sql query as a string, it should parse this query prepending "tablename" to any tables it finds. I have been given some code as an example (its simply looking for instances of "from" and "where/inner/outer/left/right" etc.. and assuming that the word in the middle is a table...
  This is being done in C#</p>
  
  <p>All Im really looking for is pointers of the best way to do this... (Although the brief is I will be passed a string and I will return a string, so please bear that in mind if you have any ideas!)</p>
  
  <p>I have tried using Sql parser for the below sample queries but it does not help me out</p>
  
  <pre><code>  //"SELECT e.last_name,       e.department_id,       d.department_name FROM   employees e       LEFT OUTER JOIN department d         ON ( e.department_id = d.department_id ); "
     //"SELECT   a.Id, a.Name  AS Surname, b.preName, c.Busrel_Id FROM  Customer a  LEFT JOIN CustomerDetail b ON a.Id = b.fId   LEFT JOIN Businessrel c            ON b.cId = c.Bid         JOIN Contract d           ON c.Id = d.fId  AND a.DATE &lt; '20071204:13.23.14.000' AND a.cst_Class_cd IN ('01','02') AND b.Name LIKE 'W%'  AND (SELECT MAX(e.TIMESTAMP) FROM   dDate e  WHERE  e.colLid = a.Id) ORDER BY a.Name, b.preName";
       //SELECT   Quantity,b.Time,c.Description FROM   (SELECT ID,Time FROM bTab) b  INNER JOIN aTab a on a.ID=b.ID  INNER JOIN cTab c on a.ID=c.ID
  </code></pre>
  
  <p>code which i used was</p>
  
  <pre><code>using gudusoft.gsqlparser;
  using gudusoft.gsqlparser.Units;
  
  TSelectSqlStatement stmt = (TSelectSqlStatement)sqlparser.SqlStatements[0];
                  foreach (TLzField lcfield in stmt.Fields)
                  {
                      lctablename = "NULL";
                      lcfieldalias = "NULL";   
  
                      if (lcfield.FieldPrefix.Length &gt; 0)
                      {
                          lctablename = findtablename(stmt.Tables, lcfield.FieldPrefix);
                      }
  
                      lcfieldname = lcfield.FieldName;
  
                      if (lcfield.FieldAlias.Length &gt; 0)
                      {
                          lcfieldalias = lcfield.FieldAlias;
                      }
  
                      Console.WriteLine("{0},{1},{2}", lctablename, lcfieldname, lcfieldalias);
                  }
  
  public static string findtablename(TLzTableList tbl, string tname)
          {
              foreach (TLzTable tb in tbl)
              {
                  if (tb.TableName.CompareTo(tname) == 0)
                  {
                      return tb.TableName;
                  }
                  else if (tb.TableAlias.CompareTo(tname) == 0)
                  {
                      return tb.TableName;
                  }
              }
              return tname;
          }
  </code></pre>
  
  <p>What ever the query listed out such as</p>
  
  <p>1)nested query
  2)sub queries
  3)joint queries</p>
  
  <p>i am unable to retrieve table name specified in that query</p>
  
  <p>waiting for your responses and comments</p>