SQL SERVER – Fix: Error Msg 128 The name is not permitted in this context. Only constants, expressions, or variables allowed her

fog911811發表於2010-07-16

Reference:http://blog.sqlauthority.com/2007/03/22/sql-server-fix-error-msg-128-the-name-is-not-permitted-in-this-context-only-constants-expressions-or-variables-allowed-here-column-names-are-not-permitted/

 

 

Error Message:

Server: Msg 128, Level 15, State 1, Line 3
The name is not permitted in this context. Only constants, expressions, or variables allowed here. Column names are not permitted.

Causes:

This error occurs when using a column as the DEFAULT value of another column when a table is created.
CREATE TABLE [dbo].[Items] (
[OrderCount] INT,
[ProductAmount] INT,
[TotalAmount] DEFAULT ([OrderCount] + [ProductAmount])
)

Executing this CREATE TABLE statement will generate the following error message:

Server: Msg 128, Level 15, State 1, Line 5
The name ‘TotalAmount’ is not permitted in this context.
Only constants, expressions, or variables allowed here.
Column names are not permitted.

Solution/Workaround/Fix:

When setting the DEFAULT value of a column make sure to use only constants, expressions or variables and not another column.
System functions can be used as DEFAULT values. e.g. GETDATE(), NEWID()
Use Computed Column in place of Default keyword.
CREATE TABLE [dbo].[Items] (
[OrderCount] INT,
[ProductAmount] INT,
[TotalAmount] AS ([OrderCount] + [ProductAmount])
)

相關文章