0
WITH
EmpDetails(EmpCode)
AS
(
 SELECT 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](EmpDetails.EmpCode,getdate(),1) WHERE YEAR(DateTaken)=year(getdate())

但我得到一个错误

消息 4104,级别 16,状态 1,行 11 无法绑定多部分标识符“EmpDetails.EmpCode”。

4

1 回答 1

0

我相信问题是您将列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 JOINEmployee.tblEmp_Demographics表执行 an 来过滤掉不必要的。

于 2012-11-06T07:26:52.720 回答