I want to add a default constraint using CREATE TABLE in AZURE SYNAPSE SQL, but I received the below error message instead. Also tried in alter statement but no luck. I have tried multiple ways but no luck. Could you please help me with the solution.
Asked
Active
Viewed 2,922 times
3

vikas madoori
- 147
- 1
- 11
-
As the error message says, you can't use expressions for default constraints in Azure Synapse. You can only use literal values and constants. – Lasse V. Karlsen Sep 30 '21 at 08:33
-
Could you suggest any solution please ?? – vikas madoori Sep 30 '21 at 08:36
1 Answers
3
As per the documentation expressions (and functions like GETDATE()
) cannot be used with defaults:
Note In Azure Synapse Analytics, only constants can be used for a default constraint. An expression cannot be used with a default constraint.
The solution is to specify the column as NOT NULL
and ensure a date is supplied when the data is inserted into the table, eg:
INSERT INTO yourTable ( someId, yourDateColumn )
SELECT someId, GETDATE()
FROM someTable;
An alternative would be to use CREATE TABLE AS (CTAS)
and specify the column value there, eg:
CREATE TABLE yourTargetTable
WITH (
DISTRIBUTION = ROUND_ROBIN,
CLUSTERED COLUMNSTORE INDEX
)
AS
SELECT someId, GETDATE() AS loadDate
FROM stagingTable;

wBob
- 13,710
- 3
- 20
- 37