ATTACHMENT 6 SQL Server 2012 Programming Standards SQL Server Object Design and Programming Object Design and Programming Idaho Department of Lands
Document Change/Revision Log Date Version Author Description of Change 11/15/2013 2 Scott Nicholson Final released 01/13/2014 3 Scott Nicholson Renamed, reformatted, and edited for accuracy and content 02/06/2014 4 Scott Nicholson Removed references to SSISAutomation and updated to reflect the Microsoft Deployment Model. 02/10/2014 5 Scott Nicholson Added recommendation to use SSIS as data migration method. 2
Contents 1 Purpose... 4 2 T-SQL Programming Conventions... 4 2.1 Database... 4 2.2 Tables... 4 2.2.1 Columns... 4 2.2.2 Referential Integrity... 4 2.2.3 Triggers... 4 2.2.4 Indexes... 4 2.3 Views... 4 2.4 Programmability... 4 2.4.1 General... 4 2.4.2 Data Modifications... 5 2.4.2.1 INSERT Statements...5 2.4.2.2 UPDATE and DELETE Statements...5 2.4.2.3 Error Handling...5 3 SSIS Programming... 5 3.1 Assemblies... 5 3.2 SSIS Package Design... 5 3.2.1 Configuration... 5 3.2.2 Connections... 5 3.2.3 Expressions/Variables... 6 3
1 Purpose This document identifies the Idaho Department of Lands (IDL) database development standards that facilitate ease of design, troubleshooting, and maintenance. It is considered a living document and modifications will be made when necessary. This guidance is applicable to all new development and applies when refactoring existing database objects. 2 T-SQL Programming Conventions 2.1 Database Submit Database alterations scripts to source control after technical review. 2.2 Tables Explicitly define a Primary Key as the Clustered Index where feasible. Explicitly define a surrogate key. 2.2.1 Columns Initially assign the smallest feasible Unicode Data Type. Foreign Key columns are same name as Foreign Table column. Not Null constraint set where feasible. 2.2.2 Referential Integrity Explicitly define Foreign Keys where possible. Triggers will not enforce integrity within the same database. Cascading Update and Delete option disabled. 2.2.3 Triggers Triggers are not used. 2.2.4 Indexes Define the Primary Key as the clustered index on the table. Define Index on Foreign Key columns. Specify Index Fill Factor property. Explicitly define a Clustered Index where feasible. Define Index with the most selective (unique) column(s) first. Define Unique Indexes instead of Unique Constraints. 2.3 Views Include Brief description in Extended Properties. Upper Case all T-SQL Reserved Keywords (e.g. SELECT, FROM, WHERE, JOIN, etc.). Place a comma at the beginning of each statement line. Explicitly specify each column returned by the query. Explicitly reference table objects by the fully qualified name (e.g. [Common].[dbo].[tblCounty]). Define joins within ANSI standard. Provide meaningful table alias whenever referencing multiple tables. Consider using the AS clause when aliasing tables. Minimize use of DISTINCT keyword. Minimize use of ORDER BY clause. 2.4 Programmability 2.4.1 General Include Brief description in Extended Properties. Upper Case all T-SQL Reserved Keywords (e.g. SELECT, FROM, WHERE, JOIN, etc.). Explicitly SET NOCOUNT ON option. Place a comma at the beginning of each statement line. o Example: 4
SELECT [FirstName] = au.[userfirstname],[lastname] = au.[userlastname] FROM [Database].[dbo].[AppUser] AS au Explicitly specify each column returned by the query. Explicitly reference table objects by the fully qualified name (e.g. [Common].[dbo].[tblCounty]). Declare DDL for temporary tables and table variables at the beginning of the code. Explicitly drop temporary tables when the stored procedure is complete. Define joins within ANSI standard. Provide meaningful table alias whenever referencing multiple tables. Consider using the AS clause when aliasing tables. Minimize use of DISTINCT keyword. Minimize use of ORDER BY clause. 2.4.2 Data Modifications 2.4.2.1 INSERT Statements Encapsulate single or multiple table inserts into a single transaction. Provide explicit list for INSERT INTO statements. Consider TRY CATCH blocks with ROLLBACK TRANSACTION to prevent partial data addition. 2.4.2.2 UPDATE and DELETE Statements Encapsulate single or multiple table updates into a single transaction Consider TRY CATCH blocks with ROLLBACK TRANSACTION to prevent partial data modification. Add a line break for each column updated in the statement. Consider creating temporary tables or table variables that restrict data for large UPDATE/DELETE transactions. 2.4.2.3 Error Handling DML procedures return a value of zero to indicate success. DML procedures define only one exit point for success and one exit point for failure. 3 SSIS Programming IDL strongly recommends handling all data migration tasks within SSIS package transactions to prevent unanticipated data additions or removals from the target database. SQL Server Integration Service (SSIS) is a powerful successor to the Data Transformation Service (DTS). Many capabilities have been added and will be addressed in this document. To facilitate portability, the Microsoft deployment model has been adopted, utilizing SSISDB to hold parameters associated with each package / project. 3.1 Assemblies Any binary files being used as assemblies are located in the C:\Program Files\Microsoft SQL Server\IdlBin (IDL Binaries) folder. 3.2 SSIS Package Design Good design will provide clues to needed processes within any package. Peer review is highly encouraged to assure accuracy and prevent oversight. Flow diagrams will help with the control flow of any SSIS package. Use of annotations should be frequent and explicit. 3.2.1 Configuration Portability from the development environment to the testing environment and then to the production environment must be accurate and timely. Databases, file locations, email groups, etc. should be defined in a container that can be readily modified. The primary configuration information is contained in the database SSISDB. 3.2.2 Connections The primary Connection Manager is set up as SSISAutomation and points to the local instance of the SSISDB database. Additional connection managers should reflect type and name (if possible). 5
3.2.3 Expressions/Variables Extensive use of variables should be made to increase portability between environments. Server names, file locations, email addresses, etc. should be configured at the earliest possible time. All variables used in an SSIS package should be listed in a comment on the Control Flow of the package with an explanation of the variable and how and where it is used in the package. In addition, any object using expressions should have the expressions listed in a comment on the Control Flow with the object name and what value of the object is being set by the expression. 6