SQL coding guidelines
The purpose of these guidelines is to provide Thinkwise Platform developer with best practices for programming control procedure templates in T-SQL.
General​
- Use a domain with domain elements if you need to program on values.
- Avoid the use of the
distinct
keyword. - Avoid the use of
union
withoutall
. - Use
local static read_only forward_only
cursors. - Use
begin
andend
in IF and WHILE statements. - Use
tsf_send_message
to send messages instead ofraiserror
. - Only use
apply
when joining a table valued function or a subquery. - Make sure every task and procedure containing a
begin tran
has correspondingrollback tran
andcommit tran
statements.
Triggers​
- Avoid using triggers for complex data mutations. Do use triggers for checking functional integrity of data.
- Every statement should make use of the
inserted
ordeleted
table. - Avoid the use of cursors.
- Avoid the use of explicit transactions (
begin tran
). - Avoid the use of variables.
- Avoid the use of temporary tables.
- Don't update the
inserted
ordeleted
tables.
Defaults​
- Templates should contain an IF statement.
- Don't reset input parameter values.
- Avoid the use of cursors.
- Avoid the use of explicit transactions (
begin tran
). - Don't do inserts, updates or deletes on tables.
- Avoid the use of temporary tables.
Layouts, Contexts, Processes​
- Templates should contain an IF statement.
- Don't reset input parameter values.
- Avoid the use of cursors.
- Avoid the use of explicit transactions (
begin tran
). - Don't do inserts, updates or deletes on tables.
- Avoid the use of temporary tables.
- Don't send messages (using
tsf_send_message
orraiserror
).
Tasks and Subroutines (stored procedures)​
- Avoid the use of cursors.
- Always use explicit transactions (
begin tran
,commit tran
,rollback tran
). - When using cursors, every iteration should be a new transaction.
Subroutines (functions)​
- Avoid the use of cursors.
- Avoid the use of explicit transactions (
begin tran
). - Don't do inserts, updates or deletes on tables.
- Don't send messages (using
tsf_send_message
orraiserror
).