SQL

Table of contents

General

2018/11/30 : dbotha : 56789 : Added check for qualifications offered by my learning provider.
{Date} : {Author} : {TP#} Short concise description of change

Naming conventions

General

Note: The parent / grouping determines the module the data is stored on E.g. EmployeePDPs (an employee’s PDP’s) vs. pdpPeriod (a PDP’s periods)

Table 1 : Database Module Abbreviations

Abbreviation

Module

cc

Career Conversation

cfg

Configuration

cl

Catalogue

com

Communication (Import / Export)

cpd

Credits

dbd

Dashboards

ab

Assessment Builder

ee

Employee Evaluation

el2

E-Learning v2

el

e-Learning

els

Learning Store

em

Event Management / Training and Scheduling

fais

FAIS

ate

Ask The Expert/ Discussion Forum

icn

Icodeon

ir

Internal Relations / Disciplinary Actions

jl

Job Leveling

jp

Job Profiler

leave

Leave Management - New

lic

Licences

mc

Mentors and Coaches

ntf

Notifications

org

Organisational Structure

pdm

Performance Management

pdp

Personal Development Plan

prc

HR Processes

prs

Personnel

pw

Pathways

rb

Report Builder

rec

Recruitment

rem

Remuneration

rp

Resource Planning

rpt

Reports / Report Management

sms

SMS Notifications

sr

Salary Review

ss

Salary Scenario

sty

System Framework

sys

System Administration

tal

Talent Management - New

sc

Succession and Career Planning

tM

Talent Management - Old

txAudit

Auditing - Old

wf

Work Flows

tr_

Trigger

Tables
Views
User-Defined Functions
Stored Procedures
Temporary Tables
Common Table Expressions (CTE's)
Indexes
Constraints

Cursors

DECLARE @SchemaID INT

DECLARE curs CURSOR LOCAL FORWARD_ONLY STATIC READ_ONLY FOR
  SELECT
    SchemaID
  FROM cfgSchemaID WITH(NOLOCK)
  WHERE
    GETDATE() BETWEEN ValidFrom AND ValidTo
    AND SysID = 101

OPEN curs

FETCH NEXT FROM curs 
INTO @SchemaID

WHILE @@FETCH_STATUS = 0
BEGIN

     /*Do your commands for @SchemaID here*/ 

/*Get the next author.*/
FETCH NEXT FROM curs 
INTO @SchemaID
END

CLOSE curs
DEALLOCATE curs

Common table expressions (CTE's)

;WITH cteEmployees
     AS (SELECT
                Name
              , Surname
              , EmployeeNumber
         FROM
              prsEmployees WITH(NOLOCK))
     SELECT
            *
     FROM
          cteEmployees

The use of recursive CTEs

WITH Managers AS
(
--initialization
SELECT EmployeeID, LastName, ReportsTo 
FROM Employees
WHERE ReportsTo IS NULL
UNION ALL
--recursive execution
SELECT e.employeeID,e.LastName, e.ReportsTo
FROM Employees e INNER JOIN Managers m 
ON e.ReportsTo = m.employeeID
)
SELECT * FROM Managers 

Temporary tables

IF OBJECT_ID('tempdb..#TheTable') IS NOT NULL
BEGIN
/*Do Stuff*/
END
      CREATE TABLE #LeaveCycles(
           StartDate   DATETIME,
           EndDate     DATETIME,
           LeaveTypeId INT,
           Name        VARCHAR(200) COLLATE DATABASE_DEFAULT,
           CycleId     INT,
           CreatedDate DATETIME,
           EmployeeId  INT
                          )

      INSERT INTO #LeaveCycles(
             StartDate
           , EndDate
           , LeaveTypeId
           , Name
           , CycleId
           , CreatedDate
           , EmployeeId
                       )
      EXEC LeaveCalculateActiveCyclesByLeaveType
           @EmployeeId = @EmployeeId,
           @SchemaId = @SchemaId,
           @LeaveTypeId = @LeaveTypeId,
           @IsHistoric = @IsHistoric

 

Table variables

https://stackoverflow.com/questions/11857789/when-should-i-use-a-table-variable-vs-temporary-table-in-sql-server 

DECLARE @product_table TABLE (
    product_name VARCHAR(MAX) NOT NULL,
    brand_id INT NOT NULL,
    list_price DEC(11,2) NOT NULL
);

INSERT INTO @product_table
(
    product_name,
    brand_id,
    list_price
  )
SELECT
    product_name,
    brand_id,
    list_price
FROM
    production.products
WHERE
    category_id = 1;

Tables and indexes

User defined functions

Stored procedures

SELECT EmpNo, EmpName
FROM EmployeeRecord
WHERE Salery > 1000
EXCEPT
SELECT EmpNo, EmpName
FROM EmployeeRecord
WHERE Salery > 2000
ORDER BY EmpName;

Triggers

Existence checks

IF NOT EXISTS
              (
              SELECT TOP 1
                     1
              FROM
                   sys.all_columns c
                   JOIN sys.tables t
                      ON t.object_id = c.object_id
              WHERE t.name = 'EmployeeLeave'
                    AND c.name = 'ActionStatus')
   BEGIN
      ALTER TABLE EmployeeLeave
      ADD
                  ActionStatus INT
END
IF EXISTS
              (
              SELECT TOP 1
                     1
              FROM
                   sys.all_columns c
                   JOIN sys.tables t
                      ON t.object_id = c.object_id
              WHERE t.name = 'EmployeeLeave'
                    AND c.name = 'ActionStatus')
   BEGIN
      ALTER TABLE EmployeeLeave
      ALTER COLUMN ActionStatus NVARCHAR(2) NOT NULL
END

Views