tags:

views:

128

answers:

1

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:

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"

Any ideas why it won't parse my view creation sql? Thanks!

A: 

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.

Why do you want to use ParseStatementList instead of Parse or ParseBatch here?

stevehem