5

Can anyone please help me?

I am having bunch of .sql files. I want to parse (validate) those files before executing them on server.

I have many online sites which parse the code. But I want to parse the code using C#.

So please can you guide. If there is any tool, dll I need to use.

I just want to parse the file and not execute it

4
  • Do you truly want to fully parse the SQL file? Or just verify whether it is valid SQL that won't error when executed? Commented Jun 1, 2010 at 18:44
  • 1
    @BradC - or at least, won't throw a parse error when executed ;p Commented Jun 1, 2010 at 18:45
  • do you want to validate syntax of sql? Commented Jun 1, 2010 at 18:46
  • This may be related: stackoverflow.com/questions/589096/parsing-sql-code-in-c Commented Jun 1, 2010 at 18:57

7 Answers 7

13

A way to do it is to execute it on the server, except with SET PARSEONLY ON (so that it's parsed but not executed): I think that's how the MS Query Analyzer does it.

Sign up to request clarification or add additional context in comments.

Comments

3

You can have procedure which can get the query as input and
in SQL Server side you can have a SET PARSEONLY ON statement and then "run" the query. The query won't actually run, but any errors will be reported.

Comments

1

Antler Parser Generator is your best bet. There might be a pre-defined grammar for the version of SQL you want to parse already.

Comments

1

You should ask yourself twice (or maybe even three times :-)) if you really want to do this.

Even if you find a solution, that seems to work for the files you have now, there will probably be all sorts of corner cases that wont be covered. The suggestion of having a local database engine parse it, is sort-of workable, but there can still be many small syntax differences between what you do locally and what happens on the server.

My advice is to let the server parse and validate it - it's basically the only way to be really sure.

Comments

1

The DMS Software Reengineering Toolkit has SQL parsers, used to build many SQL-based tools.

One such tool is a FormatterFormatter, which parses the source text and formats the result nicely. If the formatter fails to parse the source, it produces a nonzero process status.

You could simply "format" a file; if the formatting fails, it isn't legal syntax.

These formatters are commercial products.

EDIT Aug 2011: DMS SQL parser now handles full SQL 2011 standard grammar.

Comments

0

A quick google threw up http://www.antlr.org/ with a plugin for ms sql or one of the other sql dialects

Comments

0

Visual Studio used to have something called Visual Studio for Database Professionals. It was later rolled into Team Developer.

Anyway, they "compiled" the sql code by executing it in a database and testing the results. I imagine if that's how they were verifying it worked, it's probably the best way for you to do as well.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.