sqlserver-kitSQL Server Name Convention and T-SQL Programming Stylemd
sqlserver-kitSQL Server Name Convention and T-SQL Programming Stylemd
md
/C:/Users/rodri/OneDrive/BC/sqlserver-kit_SQL Server Name Convention and T-SQL Programming Style.md at master · ktaranov_sqlserver-kit · GitHub.html
ktaranov
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign up
Branch: master
There are only two hard things in Computer Science: cache invalidation and naming things -- Phil Karlton
Naming convention is a set of rules for choosing the character sequence to be used for identifiers which denote variables, types, functions, and other
entities in source code and documentation.
Reasons for using a naming convention (as opposed to allowing programmers to choose any character sequence) include the following:
Table of Contents
SQL Server Object Name Convention
SQL Server Data Types Recommendation
Reference and useful links
Char
Object Code Notation Length Plural Prefix Suffix Abbreviation Mask Example
1/10
Char
Object Code Notation Length Plural Prefix Suffix Abbreviation Mask Example
2/10
Char
Object Code Notation Length Plural Prefix Suffix Abbreviation Mask Example
⬆ back to top
What use
General Type Type ANSI Recommended instead Why use or not
Exact Numerics bit No Maybe tinyint bit convert any number (except 0) to 1, 0 converted to 0
Exact Numerics tinyint No Maybe int for saving 3 bytes compare to int data type or for replacing
bit data type
Exact Numerics smallint Yes Maybe int for saving 2 bytes compare to int data type
Exact Numerics smallmoney No Maybe decimal possibility to loss precision due to rounding errors
Exact Numerics money No Maybe decimal possibility to loss precision due to rounding errors
Approximate float(1-24) Yes No real SQL Server automatically converts float(1-24) to real data
Numerics type
Date and Time datetime Yes Maybe datetime2 On the Advantages of DateTime2(n) over DateTime
Character Strings char Yes Maybe varchar Save 1 byte from varchar , but be ready for trailing spaces
Character Strings text No Deprecated varchar(max) Differences Between Sql Server TEXT and VARCHAR(MAX)
Data Type
3/10
What use
General Type Type ANSI Recommended instead Why use or not
Binary Strings binary Yes Deprecated varbinary Conversions between any data type and the binary data types
are not guaranteed
Other Data Types timestamp No Deprecated rowversion it is just synonym to rowversion data type and must be removed
⬆ back to top
For database objects names in code use only schema plus object name, do not hardcode server and database names in your code:
dbo.MyTable is good and bad PRODSERVER.PRODDB.dbo.MyTable . More details here, here and here.
Avoid using asterisk in select statements SELECT * , use explicit column names. More details here.
No square brackets [] and reserved words in object names and alias, use only Latin symbols [A-z] and numeric [0-9] .
Prefer ANSI syntax and functions (CAST instead CONVERT, COALESE instead ISNULL, etc.).
All finished expressions should have semicolon ; at the end. This is ANSI standard and Microsoft announced with the SQL Server 2008 release
that semicolon statement terminators will become mandatory in a future version so statement terminators other than semicolons (whitespace) are
currently deprecated. This deprecation announcement means that you should always use semicolon terminators in new development. More
details here.
Data types declaration should be in lowercase: varchar(30) , int , real , nvarchar(max) etc. More details here.
All system database and tables must be in lowercase for properly working for Case Sensitive instance: master, sys.tables … .
4/10
Avoid non-standard column aliases, use, if required, double-quotes for special characters and always AS keyword before alias:
SELECT
p.LastName AS "Last Name"
FROM dbo.Person AS p;
More details here. All possible ways using aliases in SQL Server:
SELECT
FirstName
Arguments are divided by line breaks, commas should be placed before an argument:
SELECT
FirstName
, LastName
For SQL Server >= 2012 use FETCH-OFFSET instead TOP. More details here. But if you use TOP avoid use TOP in a SELECT statement without an
ORDER BY . More details here.
If you using TOP (instead recommended FETCH-OFFSET) function with round brackets because TOP has supports use of an expression, such as
(@Rows*2) , or a sub query: SELECT TOP(100) LastName … . More details here. Also TOP without brackets does not work with UPDATE and
DELETE statements.
For demo queries use TOP(100) or lower value because SQL Server uses one sorting method for TOP 1-100 rows, and a different one for
101+ rows. More details here.
Avoid specifying integers in the ORDER BY clause as positional representations of the columns in the select list. The statement with integers is
not as easily understood by others compared with specifying the actual column name. In addition, changes to the select list, such as changing the
column order or adding new columns, requires modifying the ORDER BY clause in order to avoid unexpected results. More details here.
/* bad */
SELECT ProductID, Name FROM Production.Production ORDER BY 2;
/* good */
SELECT ProductID, Name FROM Production.Production ORDER BY Name;
Avoid using ISNUMERIC function. Use for SQL Server >= 2012 TRY_CONVERT function and for SQL Server < 2012 LIKE expression:
Avoid using INSERT INTO a permanent table with ORDER BY . More details here.
Avoid using shorthand ( wk, yyyy, d etc.) with date/time operations, use full names: month, day, year . More details here.
5/10
Avoid ambiguous formats for date-only literals, use CAST('yyyymmdd' AS DATE) format.
Avoid treating dates like strings and avoid calculations on the left-hand side of the WHERE clause. More details here.
Avoid using hints except RECOMPILE if needed and NOEXPAND (see next tip). More details here.
Use NOEXPAND hint for indexed views on non enterprise editions and Prior to SQL Server 2016 (13.x) SP1 to let the query optimizer know that we
have indexes. More details here.
Avoid use of SELECT…INTO for production code, use instead CREATE TABLE + INSERT INTO … approach. More details here.
Use only ISO standard JOINS syntaxes. The old style Microsoft/Sybase JOIN style for SQL, which uses the =* and *= syntax, has been
deprecated and is no longer used. Queries that use this syntax will fail when the database engine level is 10 (SQL Server 2008) or later
(compatibility level 100). The ANSI-89 table citation list ( FROM tableA, tableB ) is still ISO standard for INNER JOINs only. Neither of these
styles are worth using. It is always better to specify the type of join you require INNER , LEFT OUTER , RIGHT OUTER , FULL OUTER and
CROSS , which has been standard since ANSI SQL-92 was published. While you can choose any supported JOIN style, without affecting the
query plan used by SQL Server, using the ANSI-standard syntax will make your code easier to understand, more consistent, and portable to other
relational database systems. More details here.
Do not use a scalar user-defined function (UDF) in a JOIN condition, WHERE search condition, or in a SELECT list, unless the function is
schema-bound. More details here.
Use EXISTS or NOT EXISTS if referencing a subquery, and IN or NOT IN when have a list of literal values. More details here.
Always specify a length to any text-based data type such as varchar , nvarchar , char , nchar :
/* bad */
DECLARE @myBadVarcharVariable varchar;
DECLARE @myBadNVarcharVariable nvarchar;
DECLARE @myBadCharVariable char;
DECLARE @myBadNCharVariable nchar;
/* good */
DECLARE @myGoodVarchareVariable varchar(50);
DECLARE @myGoodNVarchareVariable nvarchar(90);
DECLARE @myGoodCharVariable char(7);
DECLARE @myGoodNCharVariable nchar(10);
Use only ORIGINAL_LOGIN() function because is the only function that consistently returns the actual login name that we started with regardless
of impersonation. More details here.
6/10
Always use IF statement with BEGIN-END block to prevent errors with multi line statements:
DECLARE @x int = 0;
DECLARE @y int = 1;
/* bad */
IF @y > @x
SET @x = @x + 1;
SET @y = @y - 1;
ELSE
PRINT(1);
/* Msg 156, Level 15, State 1, Line 8
Incorrect syntax near the keyword 'ELSE'. */
/* good */
IF @y > @x
BEGIN
SET @x = @x + 1;
SET @y = @y - 1;
END;
ELSE
BEGIN
PRINT(1);
END;
FROM, WHERE, INTO, JOIN, GROUP BY, ORDER BY expressions should be aligned so, that all their arguments are placed under each other
(see Example below)
WITH CTE_MyCTE AS (
SELECT
t1.Value1 AS Val1
, t1.Value2 AS Val2
, t2.Value3 AS Val3
INNER JOIN dbo.Table3 AS t2
ON t1.Value1 = t2.Value1
WHERE t1.Value1 > 1
AND t2.Value2 >= 101
)
SELECT
t1.Value1 AS Val1
, t1.Value2 AS Val2
, t2.Value3 AS Val3
INTO #Table3
FROM CTE_MyCTE AS t1
ORDER BY t2.Value2;
⬆ back to top
All stored procedures and functions should use ALTER statement and start with the object presence check (see example below)
ALTER statement should be preceded by 2 line breaks
Parameters name should be in camelCase
Parameters should be placed under procedure name divided by line breaks
After the ALTER statement and before AS keyword should be placed a comment with execution example
The procedure or function should begin with parameters checks (see example below)
Create sp_ procedures only in master database - SQL Server will always scan through the system catalog first
Always use BEGIN TRY and BEGIN CATCH for error handling
Always use multi-line comment /* */ instead in-line comment --
Use SET NOCOUNT ON; for stops the message that shows the count of the number of rows affected by a Transact-SQL statement and
decreasing network traffic. More details here.
Do not use SET NOCOUNT OFF; because it is default behavior
Use RAISERROR instead PRINT if you want to give feedback about the state of the currently executing SQL batch without lags. More details
here and here.
All code should be self documenting
T-SQL code, triggers, stored procedures, functions, scripts, should have a standard comment-documentation banner:
7/10
<documentation>
<summary>Get all databases meta data using dynamic T-SQL</summary>
<returns>1 data set: temp table #DatabaseInfo.</returns>
<issues>No</issues>
<author>Konstantin Taranov</author>
<created>2018-03-01</created>
<modified>2019-11-14 by Konstantin Taranov</modified>
<version>1.2</version>
<sourceLink>https://github.com/ktaranov/sqlserver-kit/blob/master/Scripts/Databases_Report.sql</sourceLink>
</documentation>
⬆ back to top
BEGIN TRY
IF (@parameterValue1 < 0 OR @parameterValue2 NOT IN ('SIMPLE', 'BULK', 'FULL'))
RAISERROR('Not valid data parameter!', 16, 1);
IF (@debug) PRINT @parameterValue2;
END TRY
BEGIN CATCH
/* Print error information. */
PRINT 'Error: ' + CAST(ERROR_NUMBER()) AS varchar(50)) +
', Severity: ' + CAST(ERROR_SEVERITY(), varchar(5)) +
', State: ' + CAST(ERROR_STATE(), varchar(5) ) +
', Procedure: ' + COALESCE(ERROR_PROCEDURE(), '-') +
', Line: ' + CAST(ERROR_LINE(), varchar(5)) +
', User name: ' + CAST(ORIGINAL_LOGIN(), sysname);
PRINT ERROR_MESSAGE();
END CATCH;
GO
⬆ back to top
Dynamic SQL is a programming technique that allows you to construct SQL statements dynamically at runtime. It allows you to create more general
purpose and flexible SQL statement because the full text of the SQL statements may be unknown at compilation. For example, you can use the
dynamic SQL to create a stored procedure that queries data against a table whose name is not known until runtime.
8/10
Do not use nvarchar(max) for your object’s name parameter, use sysname instead (synonym for nvarchar(128) except that, by default, sysname
is NOT NULL).
/* Bad */
DECLARE @tableName nvarchar(max) = N'MyTableName';
/* Good */
DECLARE @tableName sysname = N'MyTableName';
/* Bad */
DECLARE @tsql nvarchar(max);
DECLARE @tableName sysname = N'My badly named table!';
SET @tsql = N'SELECT object_id FROM ' + @tableName;
/* Good */
DECLARE @tsql nvarchar(max);
DECLARE @tableName sysname = N'My badly named table 111!';
SET @tsql = N'SELECT object_id FROM ' + QUOTENAME(@tableName);
Always use sp_executesql instead EXEC to prevent sql injection. Also sp_executesql can parameterizing your dynamic statement that means
plans can be reused as well (when the value of the dynamic object is the same). Also sp_executesql can even be used to output values as well
(see example below).
/* Bad */
DECLARE @tsql nvarchar(max);
DECLARE @id int = 2107154552;
SET @tsql = N'SELECT object_id, "name" FROM master.sys.tables WHERE object_id = ' + CAST(@id AS nvarchar(max));
EXEC sp_executesql @tsql;
/* Good */
DECLARE @id int = 2107154552;
SELECT object_id, "name" FROM master.sys.tables WHERE object_id = @id;
Do not debug the code that creates the dynamic T-SQL first, debug the generated T-SQL statement instead. Use @debug variable to print (or a
SELECT statement if your dynamic T-SQL is over 4000 characters) dynamic statement instead executing it. See example below.
9/10
Do take the time to format your dynamic T-SQL.
⬆ back to top
Details
10/10