سؤال

In the MSDN Library or the Technet website, Microsoft tend to use a pseudo syntax in explaining how to use T-SQL statements with all available options. Here is a sample taking from the Technet page on UPDATE STATISTICS :

UPDATE STATISTICS table_or_indexed_view_name 
    [ 
        { 
            { index_or_statistics__name }
          | ( { index_or_statistics_name } [ ,...n ] ) 
                }
    ] 
    [    WITH 
        [ 
            FULLSCAN 
            | SAMPLE number { PERCENT | ROWS } 
            | RESAMPLE 
            | <update_stats_stream_option> [ ,...n ]
        ] 
        [ [ , ] [ ALL | COLUMNS | INDEX ] 
        [ [ , ] NORECOMPUTE ] 
    ] ;

<update_stats_stream_option> ::=
    [ STATS_STREAM = stats_stream ]
    [ ROWCOUNT = numeric_constant ]
    [ PAGECOUNT = numeric_contant ]

How to properly read such description and quickly figure out what is required and what is optional and a clean way to write your query?

هل كانت مفيدة؟

المحلول

You should refer to this Transact-SQL Syntax Conventions

The first table in that article explains pretty much everything.

In your example we can see the following:

UPDATE STATISTICS table_or_indexed_view_name 

UPDATE STATISTICS is the keyword used table_or_indexed_view_name is the name of the table or the view to update statistics for

[ 
    { 
        { index_or_statistics__name }
      | ( { index_or_statistics_name } [ ,...n ] ) 
            }
] 

This is optional [], but if supplied, you have to put a statistic name {index_or_statistics__name}, or | a list of statistic names separated by commas { index_or_statistics_name } [ ,...n ]

[    WITH 
    [ 
        FULLSCAN 
        | SAMPLE number { PERCENT | ROWS } 
        | RESAMPLE 
        | <update_stats_stream_option> [ ,...n ]
    ] 
    [ [ , ] [ ALL | COLUMNS | INDEX ] 
    [ [ , ] NORECOMPUTE ] 
] ;

This is optional too []. If used then you must begin with a WITH and you have 4 options that you must choose from. Your options are

  1. FULLSCAN
  2. SAMPLE number { PERCENT | ROWS }, where you have to define the number and you must choose from PERCENT or | ROWS
  3. RESAMPLE
  4. ` [ ,...n ]' which is a list separated by commas

Then you have to choose either ALL, COLUMNS or INDEX and preside that with a comma if you have used the WITH.

Lastly you have another option to use the NORECOMPUTE and put a comma before it if you have used any other option before it.

<update_stats_stream_option> ::=
[ STATS_STREAM = stats_stream ]
[ ROWCOUNT = numeric_constant ]
[ PAGECOUNT = numeric_contant ]

These are the list of predefined options you may use where <update_stats_stream_option> is used before (in 4).

نصائح أخرى

Any thing between Square Brackets [...] are Optional

Any thing seperated by the pipe | symbol is a one or the other option.

In your above example, you could read it as

 UPDATE STATISTICS table_or_indexed_view_name 
 [ optionally specify an index as well]
 [ optionally specify options using **WITH**

      If you use WITH then you can follow it with one of the following keywords
      FULLSCAN 
        OR SAMPLE number { PERCENT | ROWS } 
        OR RESAMPLE 
 ].. and so on
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top