Existence checks
- Make use of the existence checking definedĀ here
- Always check for existence when adding new objects to the database (see example below)
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
- Also check for existence when editing a database object (see example below)
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