sql server development guidelines
TRANSCRIPT
-
8/16/2019 SQL Server Development Guidelines
1/4
SQL Server DevelopmentGuidelines
This document summarizes SQL Server development best practices and
standards for EPAY.
Objects and Naming. Avoid pre!"in# stored procedures $ith %sp&'. SQL Server $ill !rst loo(
for this stored procedure in the master database and then in the user
database.
). Avoid usin# SQL Server (e*$ords as identi!ers +ob,ect or column
names-. f *ou encounter a (e*$ord used as an identi!er/ enclose it in
0s1uare2 0brac(ets2.
3. All tables should have a primar* (e* constraint. This $ill create a
uni1ue/ clustered inde" to enforce the primar* (e*.
4. Al$a*s use the follo$in# pattern to script code5based database ob,ects
such as stored procedures/ vie$/ functions/ etc.
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBECT_I!(N"#dbo$.
#%d&i'_st%t_)oi'_t%ce$"++
EXEC ("CRE,TE -ROCE!RE #dbo$.#%d&i'_st%t_)oi'_t%ce$,S SELECT /"+0
1O,LTER -ROCE!RE #dbo$.#%d&i'_st%t_)oi'_t%ce$,SSELECT "T2is is t2e %ct3%) code"01O
4* 1%'t 5e&issio's 2ee *41O
This $ill either create or alter the ob,ect dependin# on $hether it e"ists.
t $ill also preserve an* securit* information $ould be lost on a D67P
and 86EATE.
9. Al$a*s pre!" ob,ects $ith the schema name $hen creatin# or
referencin# them. This improves the 1uer* plan reuse and prevents an*
ob,ects from bein# created and %o$ned' b* *ou.
-
8/16/2019 SQL Server Development Guidelines
2/4
Object Design. All identit* columns and tri##ers should include the :7T ;76
6EPL8AT7: (e*$ords. it ,ust uses the value from the publisher.
b. 7n a tri##er/ it prevents the tri##er from !rin# on the subscriber.
The tri##er should onl* !re on the publisher and an* data chan#es
from the ori#inal data chan#e and the tri##er?s chan#es are
pushed usin# replication. This is especiall* true if the tri##erpushes data to another database that ma* not be replicated.
Checklist for Creating or Altering TablesPlease consider the follo$in# issues=
. All create table scripts should be schema 1uali!ed. ;or e"ample/ the
Terminal table in the database so it should be referred to as
dbo.Terminal. f *ou don?t schema 1ualif* the table *ou $ill often end up
$ith a table *ou o$n.
). An* DE:TTY columns should include the :7T ;76 6EPL8AT7: clause.
3. An* tri##ers should include the :7T ;76 6EPL8AT7: clause.
4. Does this table need auditin#@
9. An* ALTE6 TALE script $ill loc( the table. Addin# columns usuall*
means re$ritin# the table behind the scenes. Dependin# on the size of
the table this ma* impact production.
Modifying Data. All :SE6T statements into permanent tables should include the list of
columns in the :SE6T statement. This is so that addin# columns or
chan#in# column order $on?t brea( *our insert statement.
). Bse S87PE&DE:TTY+- instead of CCDE:TTY. The value of
CCDE:TTY ma* be incorrect if the table has a tri##er that inserts into
another table $ith an identit* value.
-
8/16/2019 SQL Server Development Guidelines
3/4
3. Don?t use a truncate statement. This $ill could potentiall* brea( SQL
Server replication. Either DELETE and reload the ro$s or use a E6GE
statement to ,ust clean up the table. f this causes a performance issue/
please consult *our friendl* DA.
Queries, W!"! and #O$N clauses. Avoid ,oins across lin(ed servers. t is preferable to pull the re1uired
ro$s across to a temporar* table and ,oin to that. f multiple tables are
needed from the remote server/ use a vie$ or stored procedure on the
remote server or 7PE:QBE6Y and pull ro$s from that.
). Avoid usin# functions based on columns in a
-
8/16/2019 SQL Server Development Guidelines
4/4
O. Don?t use 87ALES8E or S:BLL to handle option parameters. The
follo$in# code patterns should be avoided at all costs=