Using SQL-DMO to Check Code Syntax

I need some help with the SQL-DMO library. I'm developing a SQL-DMO application in which I need to verify the syntax of a SQL DDL query much the same way that Query Analyzer verifies syntax when you create stored procedures and views.We aren't using Query Analyzer, but want to check the syntax of stored procedures when we create them in Enterprise Manager. For example, I'm working on a project that uses Visual Basic (VB) and SQL-DMO and includes an interface through which I can alter objects such as tables and stored procedures. If I have a text statement such as

ALTER TABLE Test ADD Test_id int

I want to be able to validate the statement's syntax by using a call such as

CALL
  xObject.Databases.Item(i).Exec
  uteImmediate(Text1.Text)

in which xObject is an instance of SQLDMO.SqlServer2 and Text1.Text contains the statement I'm checking. Is there a method or function in the SQL-DMO library that will let me perform this check?

You can turn on SET PARSEONLY (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_setset_47eh.asp), which checks the syntax of each T-SQL statement and returns any error messages without compiling or executing the statement. In addition, the SQLDMO_EXEC_TYPE.SQLDMOExec_ParseOnly constant (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sqldmo/dmoref_cnst05_7ecl.asp) supports the ExecuteImmediate SQL-DMO method, altering the execution behavior or interpretation of the statement submitted for execution.

If you're using SQL-DMO as designed, you should code the DDL in terms of setting attributes to objects (in which case you'd be protected from passing badly formed SQL in the first place). For example, you'd create a new table object and call InsertColumn, passing a column object to add a column. The SQL-DMO classes will give you an exception if you break syntax rules.

—Gert Drapers
Development Manager
Visual Studio Team System

—Patrick Conlan
Platform Program Manager
Microsoft Project Team

Please or Register to post comments.

IT/Dev Connections

Las Vegas
September 30th - October 4th

Paul ThurottOur Experts will show you:
• Common SQL Server
Problems
• Best Practices for T-SQL
• SQL Server Integration
Services
• Database Development

Come See Michael Otey & Tim Ford in Person!

Early Registration Now Open

From the Blogs
May 21, 2013
blog

A Common Misconception about MAXDOP

Out of the box, SQL Server is (and has been) able to take advantage of multiple processors/cores without any effort on behalf of administrators....More
May 9, 2013
blog

My ISO 8601-Compliant Signature 2

My family recently just "officially" announced that we're in the process of adopting a child from South Africa. We're quite excited, of course, but there's a ton of paperwork to do—along with the need for gobs of signatures....More
May 8, 2013
blog

Use SSIS for ETL from Hadoop

In this blog post, Mark Kromer walks you through using SSIS as a way to use ETL techniques using Microsoft's Hadoop on Windows (HDInsight) as a source using Hive connectors...More
SQL Server Pro Forums

Get answers to questions, share tips, and engage with the SQL Server community in our Forums.