This is an example of creating a custom analysis rule with SQL Enlight that checks T-SQL code for CREATE TABLE statements that do not have a required column in the table definition.

In our example, the required column will be CreationDate and these are the steps for creating the rule:

  1. Open SQL Server Management Studio
  2. Prepare a new script or find an existing script that represents the expected code and one that violates the rule. In our case, we have two tables – table1 created with the CreationDate column and table2 created without the required column.
  3.  Select the CreationDate from the first table and use the editor’s context menu to select SQL Enlight -> Create Simple Analysis rule menu item.
  4. The analysis rule designer is opened with a test script and a match expression preset to the XPath expression matching the CreatoinDate column.
  5. Go to the Properties tab and set the rule name, message, and description fields.
  6. Go to Match Expression, make sure that the CreationDate column is selected, and click the Add Expression button.
  7. The initial match expression matches the CreationDate column inside CREATE TABLE statements:
    $batch-statements[sqml:is('CreateTable', .)]
    /k:create/g:brackets/g:commalist/i:common
    [@name = 'CreationDate'][sqml:is('Column', .)][pr:datatype[@name = 'datetime']][k:not/k:null]

    The expression has to be adjusted to fulfill the rule requirements:

      • Match the table name instead of the CreationDate column:
        $batch-statements[sqml:is('CreateTable', .)]
        /k:create
        [ g:brackets/g:commalist/i:common [@name = 'CreationDate'][sqml:is('Column', .)][pr:datatype[@name = 'datetime']][k:not/k:null] ]/*[self::i:* or self::pu:dot]
      • Inverse the check of the existence of a CreationDate column and match CREATE TABLE statements that do not have such a column:
        $batch-statements[sqml:is('CreateTable', .)]
        /k:create
        [ g:brackets/g:commalist
        [ not(
        i:common[@name = 'CreationDate'][sqml:is('Column', .)][pr:datatype[@name = 'datetime']][k:not/k:null] )]
        ]/*[self::i:* or self::pu:dot]
      • Update the Match custom message to reflect the current analysis rule.
  8. Test the rule expression and verify that the expected table is reported by the rule
  9. Saving the created analysis rule will make it available in the current analysis rule template.
How to create a simple analysis rule with SQL Enlight that checks for new tables created without a CreationDate column

 

The step by step guide for creating custom analysis rules with SQL Enlight is available as a .pdf document here.

If you have any questions, please contact us at our support email or use our feedback form.

 

Version 1.7.3.515 of SQL Enlight is already released and available for download.

Here is what’s new in this release:

  • Extended query execution plan analysis support
  • A new analysis rule for query execution plan analysis – EX0018
  • A new external template parameter support in the command line tool and MSBuild tasks
  • Small improvements and fixes

The new parameter is added to provide a way to set an external analysis template which to be used during analysis instead of the default one. Check the SQL Enlight documentation for the command-line tool and MSBuild task parameters.

As the Execution Plan Analysis is the most important feature update in this release, here we have prepared a separate post for it.

We will be happy to help you if you have any questions or comments.

Thank you!

The extended Execution Plan Analysis support is the most significant improvement in the latest SQL Enlight release 1.7.3.515. It allows execution plans analysis to be automated and applied not only on separate statements or batches but on all stored procedures and views in a given database.

Read more

One of the most exciting features in SQL Enlight is the ability to create your own static analysis rules. This is a very powerful feature that can be of great use and unleash almost an unlimited number of possibilities for the ones who consider trying it.

The current post is the first of a series of blog posts which to guide you through the creation of custom static code analysis rules with SQL Enlight. Each of the subsequent articles will cover the implementation of several interesting analysis rules and will point some unfortunately not so obvious features and abilities of SQL Enlight static code analysis engine.

Read more

The SQL Enlight analysis templates are XML files that contain analysis rules and analysis group definitions. The templates can be imported in SQL Enlight and this way can update or add new rules to the currently supported set of rules. Read more

Obsolete SQML elements replaced in 1.7.0.494

During the optimization of SQL Enlight, we did small changes in the SQML schema. These changes will affect only the users who have created their own custom analysis rules.

The SQML elements: pu:optional, pu:essential and pu:binding are no longer generated and are replaced by pu:semicolon and pu:dot. Read more