In this blog post, let’s learn about the error message “128 – The name “%.*s” is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.” in Microsoft SQL Server, the reason why it appears and the solution to fix it.
SQL Server Error Message
128 – The name “%.*s” is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
Reason for the Error
The SQL Server Error Msg 128 – The name “%.*s” is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted, occurs when you try to use a column name where a constant or variable expression is expected.
Let’s take an example to understand this error message better. Consider the following SQL query:
DECLARE @Author VARCHAR(100) = 'Senthil Balu' PRINT Author
When you execute the above SQL query, you will get the following error message:
Msg 128, Level 15, State 1, Line 3
The name “Author” is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
Solution
The error occurs because you are trying to print the value of the variable “Author”, but the variable has not been declared. Instead, you have declared a variable called “@Author” and assigned it the value ‘Senthil Balu’. To fix the error, you should replace “Author” with “@Author” in the PRINT statement:
DECLARE @Author VARCHAR(100) = 'Senthil Balu' PRINT @Author
This will print the value of the variable “@Author”, which is ‘Senthil Balu’.
In conclusion, SQL Server Error Msg 128 – The name “%.*s” is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted, occurs when you try to use a column name in a place where only constants, constant expressions, or variables are allowed. To fix this error, you should use a valid expression that consists of constants, constant expressions, or variables in that context.