The function below will fails when I run it the first time with the following error:
8124 (Multiple columns are specified in an aggregated expression containing an outer reference.)
The second time I run it it runs normally.
If I remove *DATEDIFF(D, e.FRADATO , @til)
it’s no problem.
ALTER FUNCTION [dbo].[fnc_2019_test]
(@m_id INT, @fra DATE, @til DATE)
RETURNS INT
AS
BEGIN
RETURN
(SELECT
SUM(ISNULL(e.FORBRUK, 0) * DATEDIFF(D, e.FRADATO, @til))
FROM
dbo.mlr_eos_avl e
WHERE
e.MÅLER_ID = @m_id
AND @fra < e.DATO
AND DATEADD(D, -1, @til) >= e.FRADATO)
END
This function works in SQL Server 2008 R2 and SQL Server 2016 without any problem.