SQL Server error 1030, "Only constants are allowed here," appears when you use a non-constant expression (like a column name, variable, or function) in a place that requires a literal value—most often in a TIME literal, a DEFAULT constraint, or a CHECK constraint. To fix it, replace the dynamic expression with a hard-coded constant, or move the logic into a computed column, trigger, or application code. For example, change DEFAULT GETDATE() to DEFAULT '2025-01-01 00:00:00' if a fixed value is acceptable, or use a DATETIME column with DEFAULT GETDATE() instead of a TIME column with a function.
What You'll Need
- SQL Server Management Studio (SSMS) or Azure Data Studio connected to your database.
- ALTER permission on the table you are modifying.
- The exact error message and the statement that triggered it (check the Messages tab in SSMS).
Step-by-Step
- Identify the failing statement. Run the statement that produced error 1030. In SSMS, the error appears in the Messages tab with the line number. Common culprits are
CREATE TABLEorALTER TABLEstatements that define aDEFAULTorCHECKconstraint, or anINSERT/UPDATEthat uses aTIMEliteral incorrectly. - Check for non-constant expressions in constraint definitions. Look for functions like
GETDATE(),SYSDATETIME(),CURRENT_TIMESTAMP, or column references inside aDEFAULTorCHECKclause. SQL Server requires these to be constants (literals) forTIMEcolumns. For example,DEFAULT GETDATE()on aTIMEcolumn is invalid becauseGETDATE()returns aDATETIME, not a constantTIME. - Replace the expression with a constant. If a fixed time is acceptable, use a string literal like
'08:00:00'or'14:30:00'. For aDEFAULT, writeDEFAULT '08:00:00'. For aCHECK, ensure the comparison uses constants, e.g.,CHECK (StartTime >= '08:00:00' AND StartTime <= '17:00:00'). - If you need dynamic values, change the column type or use a trigger. A
TIMEcolumn cannot have aDEFAULTthat calls a function. Instead, use aDATETIMEorDATETIME2column withDEFAULT GETDATE(), or add anAFTER INSERTtrigger that sets theTIMEvalue usingCAST(GETDATE() AS TIME). Note that triggers add complexity and can affect performance. - For
INSERT/UPDATEstatements, use a literal or parameter. If you wroteINSERT INTO Table (TimeCol) VALUES (GETDATE()), change it toVALUES (CAST(GETDATE() AS TIME))or pass a parameter like@timeParamwith aTIMEvalue. The error occurs becauseGETDATE()is not a constantTIMEliteral. - Apply the fix and re-run. After editing, execute the statement again. If the error persists, double-check that all
DEFAULTandCHECKconstraints on the table use only literals forTIMEcolumns. You can script the table to see all constraints: right-click the table in SSMS, choose Script Table as > CREATE To > New Query Editor Window. - Test with a simple insert. Insert a row without specifying the
TIMEcolumn to verify the default works, e.g.,INSERT INTO Table (OtherCol) VALUES ('test'). Then select the row to confirm the time value is set correctly.
Common Mistakes
- Using
GETDATE()orCURRENT_TIMESTAMPas a default for aTIMEcolumn—these returnDATETIME, not a constantTIME. - Forgetting that
CHECKconstraints also require constants; using a column name or variable in the expression triggers error 1030. - Assuming the error is about the
TIMEdata type only—it can occur with any data type when a non-constant is used where a constant is required. - Not qualifying string literals with the correct format;
TIMEliterals should be in'hh:mm:ss'format (24-hour clock).
Tips
- Use
CAST(GETDATE() AS TIME)inINSERT/UPDATEstatements, not inDEFAULTconstraints. - If you need a default that reflects the current time, consider using a
DATETIME2column and extracting the time portion in queries. - Script out existing constraints before altering to avoid losing them.
- Test changes on a development copy of the database first.
Troubleshooting
If the error persists after replacing expressions with constants, check for hidden constraints or computed columns that reference the TIME column. Use sp_help on the table to list all constraints. If you are using an ORM or code generator, ensure it is not injecting functions into DEFAULT clauses. For INSERT statements, verify that parameters are correctly typed as TIME and not as DATETIME. If you cannot change the column type, consider using a trigger, but be aware of potential recursion and performance impact. Always back up the database before making schema changes.
Frequently Asked Questions
What causes SQL Server error 1030?
It occurs when a non-constant expression (like a function or column reference) is used where SQL Server requires a literal constant, typically in DEFAULT or CHECK constraints.
Can I use GETDATE() as a default for a TIME column?
No, because GETDATE() is not a constant and returns a DATETIME. Use a fixed time literal or change the column to DATETIME.
How do I set a default time value in a TIME column?
Use a string literal in the format 'hh:mm:ss', e.g., DEFAULT '09:00:00'.
Does error 1030 only affect TIME columns?
No, it can occur with any data type when a non-constant is used in a context requiring a constant, such as DEFAULT or CHECK constraints.
What is the alternative to using a function in a DEFAULT constraint?
Use a trigger to set the value after insert, or change the column to a type that allows function defaults like DATETIME.
Can I use a variable in a CHECK constraint?
No, CHECK constraints must use constants or deterministic functions; variables are not allowed.