'SQL nesting BEGIN END Blocks gives SQL Error (102) [closed]
I'm getting the following error for this simple piece of SQL code. Any ideas why? Any BEGIN ... END block causes this error to occur
/* SQL Error (156): Incorrect syntax near the keyword 'END'. */
DECLARE @PROCESSEDCOUNT INT = 0
WHILE (@PROCESSEDCOUNT < 10)
BEGIN
SET @PROCESSEDCOUNT +=1
IF 1=1
BEGIN
-- Stuff happens here
END
END
This is running on SQL Server - MSSQL15
Solution 1:[1]
You need more than a comment in a begin/end block.
Try the following
IF 1=1
BEGIN
SELECT @PROCESSEDCOUNT
END
Solution 2:[2]
You don't have any condition for the inner BEGIN...END. You can remove it.
DECLARE @PROCESSEDCOUNT INT = 0
WHILE (@PROCESSEDCOUNT < 10)
BEGIN
SET @PROCESSEDCOUNT +=1
-- Stuff happens here
END
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Stu |
| Solution 2 |
