简体   繁体   English

ParseStatementList在有效的SQL语句上不起作用

[英]ParseStatementList not working on valid SQL statement

I'm trying to use the TSql100Parser.ParseStatementList method to programatically parse sql statements and pull out object names. 我正在尝试使用TSql100Parser.ParseStatementList方法以编程方式解析sql语句并提取对象名称。 This is from the Microsoft.Data.Schema.ScriptDom.Sql namespace. 这来自Microsoft.Data.Schema.ScriptDom.Sql命名空间。 Here's the code: 这是代码:

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);
}

The ParseStatementList method returns null, and inserts two errors to the errors list. ParseStatementList方法返回null,并将两个错误插入错误列表。 The errors are "Invalid syntax near CREATE" and "Invalid syntax near VIEW." 错误为“ CREATE附近的语法无效”和“ VIEW附近的语法无效”。 This is weird because the same sql statement is successfully parsed by the TSql100.Parse method. 这很奇怪,因为TSql100.Parse方法成功解析了同一条sql语句。 What's also weird is that the above code successfully parses the SQL "DROP VIEW testView" 还奇怪的是,上面的代码成功解析了SQL“ DROP VIEW testView”

Any ideas why it won't parse my view creation sql? 任何想法为什么它不解析我的视图创建SQL? Thanks! 谢谢!

ParseStatementList is used for parsing statements that can be in a batch or in the body of a stored procedure. ParseStatementList用于解析可以成批处理或存储过程主体中的语句。 Statements like CREATE VIEW (as well as CREATE/ALTER proc, trigger, function, etc). 诸如CREATE VIEW之类的语句(以及CREATE / ALTER proc,trigger,function等)。 must be the only statement in the batch and thus can't be contained inside any other type of StatementList. 必须是批处理中唯一的语句,因此不能包含在任何其他类型的StatementList中。 These statements will only be parsed in the grammar rules for batches, and so ParseStatementList does not recognize them. 这些语句仅在批处理的语法规则中进行解析,因此ParseStatementList无法识别它们。

Why do you want to use ParseStatementList instead of Parse or ParseBatch here? 为什么要在这里使用ParseStatementList而不是Parse或ParseBatch?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM