【问题标题】:ParseStatementList not working on valid SQL statementParseStatementList 不适用于有效的 SQL 语句
【发布时间】:2009-04-16 14:29:37
【问题描述】:

我正在尝试使用 TSql100Parser.ParseStatementList 方法以编程方式解析 sql 语句并提取对象名称。这来自 Microsoft.Data.Schema.ScriptDom.Sql 命名空间。代码如下:

string sql = "CREATE VIEW testView AS SELECT * from testTable";
var parser = new TSql100Parser(false);
StatementList parsedStatements; IList errrors;

using (TextReader reader = new StringReader(sql)) 
{ 
       parsedStatements = parser.ParseStatementList(reader, out errors);
}

ParseStatementList 方法返回 null,并在错误列表中插入两个错误。错误是“CREATE 附近的语法无效”和“VIEW 附近的语法无效”。这很奇怪,因为 TSql100.Parse 方法成功解析了相同的 sql 语句。同样奇怪的是,上面的代码成功解析了SQL“DROP VIEW testView”

任何想法为什么它不会解析我的视图创建 sql?谢谢!

【问题讨论】:

    标签: c# .net sql parsing


    【解决方案1】:

    ParseStatementList 用于解析可以在批处理或存储过程主体中的语句。 CREATE VIEW 之类的语句(以及 CREATE/ALTER proc、触发器、函数等)。必须是批处理中的唯一语句,因此不能包含在任何其他类型的 StatementList 中。这些语句只会在批处理的语法规则中解析,因此 ParseStatementList 无法识别它们。

    为什么要在这里使用 ParseStatementList 而不是 Parse 或 ParseBatch?

    【讨论】:

      猜你喜欢
      • 2015-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-05
      相关资源
      最近更新 更多