How to fix Error 134 on Windows Server 2025 / Windows 11

Troubleshooting Guide: SQL Server Error 134 – “The variable name ‘%.*ls’ has already been declared”

Why This Error Happens

This error occurs when a T-SQL script or stored procedure attempts to declare the same variable name more than once within the same batch or procedure scope. SQL Server enforces variable name uniqueness per batch because it compiles and allocates memory for variables at parse time, and duplicate declarations create an ambiguous reference. This is typically caused by copy-pasted code blocks, merged scripts without proper cleanup, or missing GO batch separators between logical sections that each declare the same variable.

Step-by-Step Resolution Guide

Step 1: Identify the Duplicate Declaration

  • Open the script or stored procedure referenced in the error message.
  • Search for all instances of DECLARE @VariableName using Ctrl+F in SQL Server Management Studio (SSMS).
  • Note the exact variable name shown in the error output (it replaces the %.*ls placeholder in the actual error message).

Step 2: Remove or Consolidate Redundant DECLARE Statements

  • If the same variable is declared twice with the same data type, delete the second DECLARE statement entirely.
  • If the variable is needed for a different purpose in a later section, rename it (e.g., @UserID to @UserID2) to avoid scope collision.
  • For merged scripts, check for leftover DECLARE @Counter INT or @ErrorMessage NVARCHAR lines that were duplicated during copy-paste operations.

Step 3: Insert Proper Batch Separators

  • If the variables are genuinely meant to be reused across independent logical sections, insert a GO statement between them to force SQL Server to treat them as separate batches.
  • Example:
  DECLARE @Total INT = 0;
  SELECT @Total = COUNT(*) FROM Sales;
  GO
  DECLARE @Total INT = 0; -- Now valid, new batch
  • Note that variables declared before a GO will not persist into the next batch, so ensure logic doesn’t depend on cross-batch variable values.

Step 4: Validate Stored Procedure Structure

  • If the error originates inside a stored procedure (not a raw script), open the procedure definition via sp_helptext 'ProcedureName' in SSMS.
  • Confirm that no DECLARE statement is repeated within the single procedure body, since GO batch separators are not valid inside a single stored procedure.
  • Recompile the procedure after edits using ALTER PROCEDURE to apply the corrected script.

Alternative or Verification Steps

  • Run the corrected script in a new SSMS query window and confirm it executes without Error 134; check the Messages tab for a clean “Command(s) completed successfully” output.
  • If the error persists in a large script, use SET PARSEONLY ON; before execution to validate syntax and variable scope without actually running the queries, then toggle it back to OFF.