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

  1. 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 TABLE or ALTER TABLE statements that define a DEFAULT or CHECK constraint, or an INSERT/UPDATE that uses a TIME literal incorrectly.
  2. Check for non-constant expressions in constraint definitions. Look for functions like GETDATE(), SYSDATETIME(), CURRENT_TIMESTAMP, or column references inside a DEFAULT or CHECK clause. SQL Server requires these to be constants (literals) for TIME columns. For example, DEFAULT GETDATE() on a TIME column is invalid because GETDATE() returns a DATETIME, not a constant TIME.
  3. 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 a DEFAULT, write DEFAULT '08:00:00'. For a CHECK, ensure the comparison uses constants, e.g., CHECK (StartTime >= '08:00:00' AND StartTime <= '17:00:00').
  4. If you need dynamic values, change the column type or use a trigger. A TIME column cannot have a DEFAULT that calls a function. Instead, use a DATETIME or DATETIME2 column with DEFAULT GETDATE(), or add an AFTER INSERT trigger that sets the TIME value using CAST(GETDATE() AS TIME). Note that triggers add complexity and can affect performance.
  5. For INSERT/UPDATE statements, use a literal or parameter. If you wrote INSERT INTO Table (TimeCol) VALUES (GETDATE()), change it to VALUES (CAST(GETDATE() AS TIME)) or pass a parameter like @timeParam with a TIME value. The error occurs because GETDATE() is not a constant TIME literal.
  6. Apply the fix and re-run. After editing, execute the statement again. If the error persists, double-check that all DEFAULT and CHECK constraints on the table use only literals for TIME columns. 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.
  7. Test with a simple insert. Insert a row without specifying the TIME column 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() or CURRENT_TIMESTAMP as a default for a TIME column—these return DATETIME, not a constant TIME.
  • Forgetting that CHECK constraints also require constants; using a column name or variable in the expression triggers error 1030.
  • Assuming the error is about the TIME data 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; TIME literals should be in 'hh:mm:ss' format (24-hour clock).

Tips

  • Use CAST(GETDATE() AS TIME) in INSERT/UPDATE statements, not in DEFAULT constraints.
  • If you need a default that reflects the current time, consider using a DATETIME2 column 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.