Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
551 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Wise Owl coding standards for T-SQL (the SQL dialect used in SQL Server) Part three of a three-part series of blogs |
---|
A blog setting out an approach to coding in SQL to ensure consistently high standards across your organisation.
|
In this blog
This blog gives coding conventions for more advanced SQL programmers.
Either put a comment before any batch of variable declarations:
-- the minimum date
DECLARE @MinDate DATE = '20001-01'
Or put a comment to the right:
DECLARE @FirstLetter CHAR(1) = 'S' -- first letter to use
Use PascalCase for variable names, and don't be afraid to use multiple compound words. The following are good variable names:
DECLARE @EventNameSearchString VARCHAR(100)
DECLARE @EarliestAllowableEventDate DATE
Instead of using PascalCase, you could instead use camelCase. The difference is in whether you capitalise the first letter.
Lay out stored procedures/functions like this, commenting each parameter:
CREATE PROC spListEvents(
@MinDate AS DATE -- the earliest date
, @StartLetter AS CHAR(1)=NULL -- letter events must start with
)
AS
-- list out events beginning with given letter and on
-- or after given date
SELECT
e.EventName
, e.EventDate
FROM
tblEvent AS e
WHERE
e.EventDate >= @MinDate
AND (
-- if parameter not specified, ignore it
e.EventName LIKE @StartLetter + '%'
OR @StartLetter IS NULL
)
You should indent any code between two statements which act as bookends to code. For example:
BEGIN
-- do something here
END
Or:
BEGIN TRY
-- dodgy thing to try doing
END TRY
BEGIN CATCH
-- what to do if went wrong
END CATCH
Indent these, and by preference use BEGIN and END to make them clearer:
-- if today is a Tuesday, say so
IF DATEPART(weekday,GETDATE()) = 3
BEGIN
SELECT 'It''s a Tuesday!'
END
Always indent CTEs, and begin each with a comment explaining what it returns:
WITH RecentEvents AS (
-- events since 2000
SELECT
e.EventID
FROM
tblEvent AS e
WHERE
year(e.EventDate) >= 2000
),
EventsStartingWithS AS (
-- events starting with S
SELECT
e.EventID
FROM
tblEvent AS e
WHERE
EventName like 'S%'
)
-- show events satisfying both conditions
SELECT
e.EventName
, e.EventDate
FROM
RecentEvents AS re
INNER JOIN EventsStartingWithS AS se
ON re.EventID = se.EventID
INNER JOIN tblEvent AS e
ON re.EventID = e.EventID
Indent subqueries and add a comment to explain what they do:
SELECT
c.CountryName
FROM
tblCountry AS c
WHERE (
-- number of events for country more than 50
SELECT COUNT(*)
FROM tblEvent AS e
WHERE e.CountryID = c.CountryID
) > 50
For subqueries, you should also put each of the SELECT, FROM, WHERE, etc clauses on a single line, as above, to make it more obvious which is the main query and which the subquery.
For all cases where you are listing the columns in a table, indent them. For example:
-- create table of events
DECLARE @EventTable TABLE (
EventID INT IDENTITY(1,1) PRIMARY KEY
, EventName VARCHAR(100)
)
Or:
-- add event to table
INSERT INTO tblEvent(
EventName
,EventDate
,EventDetails
) VALUES (
'Blog created'
, GetDate()
, 'Blog on SQL standards published'
)
Let me know if I've left some corner of SQL unscathed!
Parts of this blog |
---|
|
Some other pages relevant to the above blogs include:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.