Limit on number of nested IF statements Is there a technical reason when T SQL gives this error?


  Msg 191Some part of your SQL statement is nested too deeply. Rewrite the query or break it up into smaller queries.


My scenario is a function with several nested IFs with ELSEs :

IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE


Changing it to the following statement doesn't raise any error:

IF (1=2) BEGIN SET @teste = 1 END
IF (1=2) BEGIN SET @teste = 1 END
IF (1=2) BEGIN SET @teste = 1 END
According to this Microsoft Docs page, nesting of IF statements is dependent on available memory.

On my test workstation, I was able to write and execute this, which is 189 levels deep:

declare @teste int;
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
IF (1=2) BEGIN SET @teste = 1 END ELSE
print 1


189 levels of nesting seems pretty bizarre to me, but then this is a bizarro world, lately.