The variable name '%.*ls' has already been declared. Variable names must be unique within a query batch or stored procedure.

Error Message:
Server: Msg 134, Level 15, State 1, Line n
The variable name ‘%.*ls’ has already been declared. Variable names must be unique within a query batch or stored procedure.

Severity level:
15.

Description:
This error message appears when you try to declare a variable that has already been declared.

Consequences:
The SQL statement cannot be parsed and further execution is stopped.

Resolution:
Error of the severity level 15 are generated by the user and can be fixed by the SQL Server user.

Versions:
All versions of SQL Server.

Example(s):
DECLARE @orderID INT
SET @orderID = 10289
SELECT *
  FROM Northwind.dbo.Orders
 WHERE OrderID = @orderID
DECLARE @orderID BIGINT

Remarks:
The above example tries the declare the variable @orderID in the last line, although it has already been declared in the first line of the batch. You can circumvent this error message by placing a GO between the SELECT and the following DECLARE. By doing so the whole batch becomes syntactically valid and finishes without any error. However you should carefully check on the logical level if the second DECLARE is necessary at all.

]]>

Leave a comment

Your email address will not be published.