我相信问题是您将列EmpDetails.EmpCode
名传递给表值函数而不是单个值的结果。你可以试试这个:
DECLARE @EmpCode nvarchar(100);
SELECT TOP 1
@EmpCode = EmpCode
FROM Employee.tblEmp_Demographics
WHERE RecordStatus='present';
select *,ROW_NUMBER() OVER (PARTITION BY EmpCode ORDER BY DateTaken DESC) AS RowNum,
(select GradeName From Master.GradeMaster where GradeCode=Grade And Deleted=0) As Grade,
SUM(Amount) OVER(PARTITION BY Grade) AS EmpAmount,
SUM(DependentAmount) OVER(PARTITION BY Grade) As DepAmount,
0 As CheckNo
from [dbo].[SELECT_AIRTICKET_DETAILS_BY_DATE](@EmpCode,getdate(),1) WHERE YEAR(DateTaken)=year(getdate());
如果您想要Employee.tblEmp_Demographics
表中多个值的结果,您可以遍历每个值(可能不是最好的),或者修改表值函数以返回包含所有 EmpCode 值的结果集,然后添加一个WHERE
子句将数据过滤为需要。
编辑
可能是这样的——注意修改后的函数返回所有 EmpCodes & 少了一个参数:
select *,ROW_NUMBER() OVER (PARTITION BY EmpCode ORDER BY DateTaken DESC) AS RowNum,
(select GradeName From Master.GradeMaster where GradeCode=Grade And Deleted=0) As Grade,
SUM(Amount) OVER(PARTITION BY Grade) AS EmpAmount,
SUM(DependentAmount) OVER(PARTITION BY Grade) As DepAmount,
0 As CheckNo
FROM [dbo].[SELECT_AIRTICKET_DETAILS_BY_DATE](getdate(),1) AS [Tickets]
INNER JOIN Employee.tblEmp_Demographics
ON tblEmp_Demographics.EmpCode = [Tickets].EmpCode
AND tblEmp_Demographics.RecordStatus = 'present'
WHERE YEAR(DateTaken) = year(getdate());
使用此解决方案,该函数将返回所有 EmpCode,但我们通过INNER JOIN
对Employee.tblEmp_Demographics
表执行 an 来过滤掉不必要的。