I'm trying to use the TSql100Parser.ParseStatementList method to programatically parse sql statements and pull out object names. This is from the Microsoft.Data.Schema.ScriptDom.Sql namespace. Here's the code:
我正在尝试使用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);
}
The ParseStatementList method returns null, and inserts two errors to the errors list. The errors are "Invalid syntax near CREATE" and "Invalid syntax near VIEW." This is weird because the same sql statement is successfully parsed by the TSql100.Parse method. What's also weird is that the above code successfully parses the SQL "DROP VIEW testView"
ParseStatementList方法返回null,并将两个错误插入错误列表。错误是“CREATE附近的语法无效”和“VIEW附近的语法无效”。这很奇怪,因为TSql100.Parse方法成功解析了相同的sql语句。还有一点很奇怪,上面的代码成功解析了SQL“DROP VIEW testView”
Any ideas why it won't parse my view creation sql? Thanks!
任何想法为什么它不会解析我的视图创建sql?谢谢!
1 个解决方案
#1
ParseStatementList is used for parsing statements that can be in a batch or in the body of a stored procedure. Statements like CREATE VIEW (as well as CREATE/ALTER proc, trigger, function, etc). must be the only statement in the batch and thus can't be contained inside any other type of StatementList. These statements will only be parsed in the grammar rules for batches, and so ParseStatementList does not recognize them.
ParseStatementList用于解析可以在批处理或存储过程正文中的语句。像CREATE VIEW(以及CREATE / ALTER proc,触发器,函数等)的语句。必须是批处理中的唯一语句,因此不能包含在任何其他类型的StatementList中。这些语句只会在批处理的语法规则中解析,因此ParseStatementList不会识别它们。
Why do you want to use ParseStatementList instead of Parse or ParseBatch here?
为什么要在这里使用ParseStatementList而不是Parse或ParseBatch?
#1
ParseStatementList is used for parsing statements that can be in a batch or in the body of a stored procedure. Statements like CREATE VIEW (as well as CREATE/ALTER proc, trigger, function, etc). must be the only statement in the batch and thus can't be contained inside any other type of StatementList. These statements will only be parsed in the grammar rules for batches, and so ParseStatementList does not recognize them.
ParseStatementList用于解析可以在批处理或存储过程正文中的语句。像CREATE VIEW(以及CREATE / ALTER proc,触发器,函数等)的语句。必须是批处理中的唯一语句,因此不能包含在任何其他类型的StatementList中。这些语句只会在批处理的语法规则中解析,因此ParseStatementList不会识别它们。
Why do you want to use ParseStatementList instead of Parse or ParseBatch here?
为什么要在这里使用ParseStatementList而不是Parse或ParseBatch?