我发现@@ROWCOUNT
当它User Defined Function
在 MS SQL Server 2019 中使用时,它不再正常工作。
我不知道这是 MS SQL 错误还是期望的行为改变。
有什么选择可以回到 2019 年以前版本的行为?与安装设置有关吗?
这是您自己的测试的代码和结果:
CREATE FUNCTION [dbo].[udfScalar](@test NVARCHAR(255)) RETURNS int
AS BEGIN
DECLARE @var AS NVARCHAR(255)
DECLARE @ROWCOUNT AS INT = NULL
select top 1 @var = 'test' from FooTable where 1 = 0
SET @ROWCOUNT = @@ROWCOUNT
RETURN @ROWCOUNT;
END
GO
DECLARE @var AS NVARCHAR(255)
DECLARE @ROWCOUNT AS INT = NULL
select top 1 @var = 'test' from FooTable where 1 = 0
SET @ROWCOUNT = @@ROWCOUNT
select @ROWCOUNT '@@ROWCOUNT in T-SQL', [dbo].[udfScalar]('test') '@@ROWCOUNT in UDF', @@VERSION 'MS SQL VERSION'
drop FUNCTION [dbo].[udfScalar]
MS SQL Server 2019 中的结果(错误):
@@ROWCOUNT in T-SQL: 0
@@ROWCOUNT in UDF: 1
MS SQL VERSION: Microsoft SQL Server 2019 (RTM) - 15.0.2000.5 (X64)
Sep 24 2019 13:48:23
Copyright (C) 2019 Microsoft Corporation
Standard Edition (64-bit) on Windows Server 2019 Standard 10.0 <X64> (Build 17763: ) (Hypervisor)
请注意,当UDF
结果1
应该是0
由于where 1 = 0
.
结果 MS SQL Server 2016 和以前的版本(正确):
@@ROWCOUNT in T-SQL: 0
@@ROWCOUNT in UDF: 0
MS SQL VERSION: Microsoft SQL Server 2016 (SP2-GDR) (KB4505220) - 13.0.5101.9 (X64)
Jun 15 2019 23:15:58
Copyright (c) Microsoft Corporation
Standard Edition (64-bit) on Windows Server 2016 Standard 10.0 <X64> (Build 14393: ) (Hypervisor)
现在我无法访问 SQL Server 2017,但我相信它是正确的。