In this blog post, let’s learn about the error message “133 – A GOTO statement references the label ‘%.*ls’ but the label has not been declared.” in Microsoft SQL Server, the reason why it appears and the solution to fix it.
SQL Server Error Message
133 – A GOTO statement references the label ‘%.*ls’ but the label has not been declared.
Reason for the Error
SQL Server Error Msg 133 occurs when a GOTO statement references a label that has not been declared in the batch or stored procedure. A label is a user-defined identifier used to mark a location within a T-SQL script or stored procedure.
Here’s an example that demonstrates this error:
BEGIN DECLARE @counter INT = 0; my_label: IF @counter < 5 BEGIN SET @counter = @counter + 1; PRINT @counter; GOTO my_other_label; END END
In this example, the code block declares a label called my_label
. However, the GOTO statement attempts to jump to a label called my_other_label
, which has not been declared. This will result in SQL Server throwing an error message similar to the following:
Solution
To resolve this error, ensure that all label references in GOTO statements correspond to correctly declared labels within the code block.