在SQL语句字符串中查找外部FROM子句的索引

时间:2011-02-25 21:46:47

标签: c# sql parsing

我需要在SQL字符串中找到实际外部'from'的索引。以下是我将要查找实际FROM:

的SQL字符串的一些示例
select * from table --easy  
select *, (select top 1 col1 from anothertable) as col1 from table
select * from table,  (select col1 from anothertable) as anothertable 
select * from table where col1=(select top 1 col1 from anothertable) 

我确信有更多有效的SQL语句可以利用子选择。我认为我需要的是一个正则表达式或解析器,它知道最外层'from'和跳过任何sub'froms'之间的区别。当然,我可能不会考虑其他陷阱来找到外部'从',所以任何输入都会受到赞赏。

3 个答案:

答案 0 :(得分:4)

您可以使用Microsoft.Data.Schema.ScriptDom。边缘粗糙的概念应用如下所示。

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Data.Schema.ScriptDom;
using Microsoft.Data.Schema.ScriptDom.Sql;

namespace ConsoleApplication1
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            string[] queries =
                {
                    @"select * from table1;",
                    /*Test multiline*/
                    @"select *, 
                              (select top 1 col1 from anothertable) as col1 from table1;"
                    ,
                    @"select * from table1,  (select col1 from anothertable) as anothertable;",
                    @"select * from table1 where col1=(select top 1 col1 from anothertable)",
                    /*Test invalid syntax ("table" is a reserved word)*/
                    @"select * from table where col1=(select top 1 col1 from anothertable)"
                };

            foreach (string query in queries)
            {
                Parse(query);
            }

            Console.ReadKey();
        }

        private static void Parse(string query)
        {
            Console.WriteLine(@"------------------------------------------");
            Console.WriteLine(@"Parsing statement ""{0}""", query);

            var parser = new TSql100Parser(false);

            IList<ParseError> errors;
            IScriptFragment result = parser.Parse(new StringReader(query), out errors);

            if (errors.Count > 0)
            {
                Console.WriteLine(@"Errors encountered: ""{0}""", errors[0].Message);
                return;
            }


            TSqlStatement statement = ((TSqlScript) result).Batches[0].Statements[0];

            if (statement is SelectStatement)
            {
                TableSource tableSource = (((QuerySpecification)((SelectStatement)statement).QueryExpression).FromClauses[0]);

                Console.WriteLine(@"Top level FROM clause at Line {0}, Column {1} (Character Offset {2})",
                                  tableSource.StartLine, tableSource.StartColumn, tableSource.StartOffset);
                Console.WriteLine();
                Console.WriteLine();

            }
        }
    }
}

输出

------------------------------------------
Parsing statement "select * from table1;"
Top level FROM clause at Line 1, Column 15 (Character Offset 14)


------------------------------------------
Parsing statement "select *,
                              (select top 1 col1 from anothertable) as col1 from
 table1;"
Top level FROM clause at Line 2, Column 82 (Character Offset 93)


------------------------------------------
Parsing statement "select * from table1,  (select col1 from anothertable) as ano
thertable;"
Top level FROM clause at Line 1, Column 15 (Character Offset 14)


------------------------------------------
Parsing statement "select * from table1 where col1=(select top 1 col1 from anoth
ertable)"
Top level FROM clause at Line 1, Column 15 (Character Offset 14)


------------------------------------------
Parsing statement "select * from table where col1=(select top 1 col1 from anothe
rtable)"
Errors encountered: "Incorrect syntax near table."

答案 1 :(得分:0)

假设您要查找的“from”是任何括号外的唯一一个,您可以逐个字母地查看字符串并计算括号(打开时为+1,关闭时为-1)。当您的括号计数器为0时,如果找到“从”,则它应该是正确的。

答案 2 :(得分:0)

考虑到,select from子句中可能出现的字段名称,别名或SP的名称也是如此。 E.g select from select from TABLE,如果TABLE包含来自。

的字段