liveBook · Manning

Style guide

As explained in 100 SQL Server Mistakes and How to Avoid Them

A coding standard is a documented set of stylistic and technical rules for writing software consistently within a project.

A style guide is a shared coding standard: documented stylistic and technical rules that make a SQL Server data-tier application consistent. It is an architectural practice, not a matter of personal preference. An imperfect common rule is more useful than each developer following a different private rule. section 2.0 section 2.5

The immediate payoff is readable code under pressure. Poorly formatted stored procedures can require substantial time merely to identify the affected database, reformat the query, and understand its intent before troubleshooting can begin. Meaningful names and consistent layout make code easier to inspect and maintain. section 2.1 section 2.7

A useful guide addresses two kinds of decisions:

  • Stylistic rules settle recurring presentation choices, such as tabs versus four spaces, semicolon termination, comma placement, JOIN and ON layout, keyword capitalization, and camel case versus Pascal case. section 2.5
  • Technical rules constrain risky or inefficient patterns, including cursors, UNION instead of UNION ALL where possible, SELECT *, NOLOCK, and DISTINCT. section 2.5
  • Team members, including DBAs writing administrative scripts, follow the same rules. Exceptions to a no-cursor rule can multiply until the standard becomes unenforceable and small workloads grow. section 9.10
  • Security-oriented rules can require stored procedure access rather than dynamically built SQL to help protect against SQL injection. section 14.8

Changing style without changing intent

The same SELECT can be formatted with aligned columns and leading commas or with a different indentation and comma convention. Neither presentation is inherently the point; choosing one convention and applying it consistently lets readers focus on the query rather than decode its layout. section 2.5

Listing 2.14 Changing code style
SELECT                                                           #1
      c.FirstName                                                #1
    , c.LastName                                                 #1
    , o.LineItems                                                #1
FROM dbo.customers c                                             #1
LEFT JOIN dbo.orders o                                           #1
    ON c.CustomerID = o.CustomerID                               #1
WHERE c.CustomerID >= 2 AND c.CustomerID <= 3                    #1
    AND c.CustomerID <> 2 ;                                      #1
  
SELECT                                                           #2
    cust.FirstName,                                              #2
    cust.LastName,                                               #2
    ord.LineItems                                                #2
FROM dbo.customers cust                                          #2
LEFT JOIN (                                                      #2
    SELECT CustomerID,                                           #2
    LineItems                                                    #2
    FROM dbo.Orders                                              #2
) ord ON cust.CustomerID = ord.CustomerID                        #2
WHERE cust.CustomerID BETWEEN 2 AND 3 AND cust.CustomerID != 2 ; #2

Compare two layouts for one SELECT statement to see which choices a style guide must settle consistently. section 2.5

How 100 SQL Server Mistakes and How to Avoid Them covers it

How deep it goes
Distinguishes stylistic from technical rules and illustrates enforcement consequences, without prescribing one universal formatting convention.
Examples
Uses an unreadable stored procedure, alternate SELECT formatting, and cursor-based index rebuilding as concrete cases.
What you can do after
Apply shared rules consistently and resist exceptions that undermine maintainability, performance, or security practices.
How it connects
Connects coding standards to database architecture, naming conventions, cursors, stored procedures, and SQL injection.

Also explained in

Compare how 10 books explain style guide →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 SQL Server Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.