Search code examples
sqlsql-serversql-server-2014

Nesting level limit error in SQL Server 2014


We have the following SQL Server stored procedure:

ALTER PROCEDURE [dbo].[up_get_id]
    (@KeyName VARCHAR(30), @ID INTEGER OUTPUT)
AS
BEGIN
    DECLARE @SEQ_NAME VARCHAR(120);
    SET @SEQ_NAME = 'seq_dbk_' + @KeyName;

    DECLARE @SQL NVARCHAR(1000);
    SET @SQL = 'set @ID = next value for ' + @SEQ_NAME;

    EXEC sp_executesql @SQL, N'@ID int out', @ID = @ID output;

    RETURN 0;
END
GO

Sometimes (not always, probably under high loading) after calling the stored procedure:

declare @P1 int
exec up_get_id  @KeyName = 'KEY', @ID = @P1 output
select @P1 Result

the client is getting an error:

Warning: Fatal error 217 occurred at Nov 21 20117 12:54PM. Note the error and time, and contact your system administrator.

Also, the following message appears in SQL Server log:

Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 2).

Any ideas of what is wrong?

Server version:

Microsoft SQL Server 2014 - 12.0.4213.0 (X64)   
Jun 9 2015 12:06:16   
Copyright (c) Microsoft Corporation  
Enterprise Edition (64-bit) on Windows NT 6.3 <X64> (Build 9600: ) (Hypervisor)

Solution

  • Аnd unanswered response :)

    If COMPATIBILITY_LEVEL 100 and less error

    Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 2).

    110 and more

    NEXT VALUE FOR function cannot be used if ROWCOUNT option has been set, or the query contains TOP or OFFSET.

    So you set ROWCOUNT <> 0