When the SQL plan has interleaved execution enabled and uses the OPTION (OPTIMIZE FOR) clause, the table-valued function (TVF) that uses a parameter passed in the same SQL statement gives incorrect results on the first run.
This problem is fixed in the following cumulative update for SQL Server:
Note Here is an example of not using interleaved execution after you install this cumulative update:
DECLARE @start smalldatetime
SELECT count(*) FROM dbo.GetData(@start)
OPTION (OPTIMIZE FOR (@start = '2022-05-23T06:00:00'))
CREATE FUNCTION [dbo].[GetData]
RETURNS TABLE AS RETURN
SELECT t.id FROM dbo.TestTable t
WHERE t.testdate >= @start
CREATE TABLE [dbo].[TestTable](
[id] [int] IDENTITY(1,1) NOT NULL,
[testdate] [datetime] NULL
) ON [PRIMARY]
Each new cumulative update for SQL Server contains all the hotfixes and security fixes that were in the previous build. We recommend that you install the latest build for your version of SQL Server:
Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section.
Learn about the terminology that Microsoft uses to describe software updates.