Share via

Function with label crashes when called from select statement

PrasannaSridharan-MSFT 96 Reputation points Microsoft Employee
2022-08-19T15:53:35.25+00:00

alter function [FOO].[TEST]
(
@P1 INT = 11,
@P2 INT = 1
)
RETURNS INT
AS
BEGIN
--BEGIN
somelabel:
--END
return @P2;
END

-- run this
select foo.test(1,2)

-- error:

Msg 596, Level 21, State 1, Line 14
Cannot continue the execution because the session is in the kill state.
Msg 0, Level 20, State 0, Line 14
A severe error occurred on the current command. The results, if any, should be discarded.

Version: Microsoft SQL Server 2019 (RTM-GDR) (KB5014356) - 15.0.2095.3 (X64) Apr 29 2022 18:00:13 Copyright (C) 2019 Microsoft Corporation Enterprise Edition (64-bit) on Windows 10 Enterprise 10.0 <X64> (Build 22000: ) (Hypervisor)

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

0 comments No comments

Answer accepted by question author

Erland Sommarskog 134.7K Reputation points MVP Volunteer Moderator
2022-08-19T21:46:00.077+00:00

You are running the RTM version of SQL 2019. There have been many bug fixes related to the inlining of scalar functions and you should download and install the most recent cumulative update, CU17.

...however, this particular issue has not been fixed. I was able to repro the error on SQL 2019 CU17.

A workaround is to turn off inlining by adding WITH INLINE=OFF after RETURNS INT.

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Viorel 127K Reputation points
    2022-08-19T16:28:07.69+00:00

    To work around the problem, try something like this:

    alter function [FOO].[TEST]  
    (  
    @P1 INT = 11,  
    @P2 INT = 1  
    )  
    RETURNS INT  
    AS  
    BEGIN  
    goto somelabel  
    somelabel:  
    return @P2;  
    END  
    

    The issue can be reported here:

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.