The following function is causing me issues. It always falls into the IF @@ROWCOUNT = 0
even when the @Result
has a value in it. I have ran these statements in isolation outside the function to confirm that there is data and the rowcount is actually 1. Yet somehow, every time the @@ROWCOUNT = 0
in the IF
.
My collegues do not get this problem, only me. They are using SQL Server 2017 and I am using 2019. Also I wonder if it could be related to the speed of my machine vs others, ie. the number of threads etc.
ALTER FUNCTION [fnc_SomeFuncion]
(
@Param1 varchar(100),
@Param2 nchar(10)
)
RETURNS INT
AS
BEGIN
DECLARE @Result bigint
SELECT @Result = v.Id
FROM vSomeView v WITH(NOLOCK)
WHERE v.X = @Param1 AND v.Y = 1 AND v.Z = @Param2
-- PRINT @Result -- prints 1
IF @@ROWCOUNT = 0 -- should be 1 but is 0
BEGIN
... -- always enters here
END
RETURN @Result -- this is a valid id but never gets returned
END