Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions docs/contributing/code-style/sql.md
Original file line number Diff line number Diff line change
Expand Up @@ -462,10 +462,8 @@ WHERE
[Column] IS NULL
GO

ALTER TABLE
[dbo].[Column]
ALTER COLUMN
[Column] INT NOT NULL
ALTER TABLE [dbo].[Table]
ALTER COLUMN [Column] INT NOT NULL
GO
```

Expand All @@ -474,14 +472,20 @@ This is better:
```sql
IF COL_LENGTH('[dbo].[Table]', 'Column' IS NULL
BEGIN
ALTER TABLE
[dbo].[Column]
ADD
[Column] INT NOT NULL CONSTRAINT DF_Table_Column DEFAULT 0
ALTER TABLE [dbo].[Table]
ADD [Column] INT NOT NULL CONSTRAINT DF_Table_Column DEFAULT 0
END
GO
```

:::warning Do not use defaults for string columns

Default values should only be used for integral types (`BIT`, `TINYINT`, `SMALLINT`, `INT`,
`BIGINT`). Do not provide default values for string columns (`VARCHAR`, `NVARCHAR`, or their `MAX`
variants), as this can lead to unnecessary storage overhead and performance issues.

:::

#### Changing a column data type

You must wrap the `ALTER TABLE` statement in a conditional block, so that subsequent runs of the
Expand Down