1 min read

T-SQL Shorts: Table Variables & Constraints

Today while working on a Uniqueness problem I discovered that
1) You can add constraints to Table Variables
2) You can not name Table Variable Constraints

Example:

USE adhoc;GO
DECLARE @TableName TABLE
(
Row_ID INT IDENTITY(1,1) NOT NULL
,CharColumn VARCHAR(4) NOT NULL
,DecColumn DECIMAL(7,0) NOT NULL
,TextColumn1 VARCHAR(500)
,Created DATETIME NOT NULL DEFAULT GETDATE()
,CreatedBy VARCHAR(30) NOT NULL DEFAULT SUSER_SNAME()
,UNIQUE (CharColumn,DecColumn)
,PRIMARY KEY CLUSTERED (CharColumn,DecColumn)
)