使用Cte

时间:2017-05-19 13:03:35

标签: sql reportviewer

我有一个查询。它在MSSQL Management Studio 2008r2上工作正常。任何错误。我将使用查询报告。所以我正在将TableAdapter添加到数据集。当我添加查询抛出异常时这样

WITH t1 AS 
(
    select  CONVERT(VARCHAR(24),tarihi, 103) tarihi,emp1.EmployeeCode,emp1.EmployeeID,emp1.EmployeeName
    from Personel.dbo.Dates d 
        cross join 
        (
            SELECT EmployeeCode,EmployeeID, EmployeeName 
            from TEmployee where EmployeeName <> '' AND  EmployeeName != 'MISAFIR'
        )   emp1 where d.tarihi between '2017-04-28' and '2017-05-05' 
),
t2 AS 
(
        SELECT 
        emp.EmployeeCode,
        CONVERT(VARCHAR(24), e.EventTime, 103) AS EventTime, 
        CONVERT(VARCHAR(24), MIN(e.EventTime), 108) AS GirisSaat, 
        CONVERT(VARCHAR(24), MAX(e.EventTime), 108) AS CikisSaat, 
        CONVERT(VARCHAR(5), MAX(e.EventTime) - MIN(e.EventTime), 108) AS total 
        FROM     
        TEvent AS e 
        LEFT OUTER JOIN TEmployee AS emp ON emp.EmployeeID = e.EmployeeID
        WHERE  (e.EventType <> 1) and EventTime between '2017-04-28' and '2017-05-05' 
        GROUP BY CONVERT(VARCHAR(24), e.EventTime, 103), e.EmployeeID, emp.EmployeeName, emp.EmployeeCode
)

 SELECT 
 t1.tarihi ,
 t1.EmployeeCode,
 t1.EmployeeName,
 ISNULL(t2.GirisSaat,'00:00:00') GirisSaat,
 ISNULL(t2.CikisSaat,'00:00:00') CikisSaat,


 ISNULL(ISNULL(t2.total,(SELECT '08:00' FROM Personel.dbo.izinli_listesi il
left join Personel.dbo.izin izn on izn.id = il.izin_id
 where 
 personel_id = t1.EmployeeID and 
 CONVERT(VARCHAR(24),t1.tarihi, 103) between CONVERT(VARCHAR(24),izin_bas_tar, 103) and CONVERT(VARCHAR(24),izin_bit_tar, 103))
  ),'00:00') total,

  isnull(CASE WHEN t2.total IS NULL 
    THEN 
        (SELECT izin_adi FROM Personel.dbo.izinli_listesi il
         left join Personel.dbo.izin izn on izn.id = il.izin_id 
          where  personel_id = t1.EmployeeID and 
          CONVERT(VARCHAR(24),t1.tarihi, 103) between CONVERT(VARCHAR(24),izin_bas_tar, 103) and CONVERT(VARCHAR(24),izin_bit_tar, 103))
    ELSE
         NULL 
    END,'')

   izindurumu  

 from t1 
  left join t2 on t2.EmployeeCode = t1.EmployeeCode and t2.EventTime=t1.tarihi
 order by t1.EmployeeCode, t1.tarihi

我认为这是关于Cte.I检查过的老问题。我没有使用&#34; *&#34;。

错误:

the multi-part identifier "t2.total" could not be bound
the multi-part identifier "t1.EmployeeID" could not be bound
the multi-part identifier "t1.tarihi" could not be bound
and again
the multi-part identifier "t1.tarihi" could not be bound

我该如何解决?

1 个答案:

答案 0 :(得分:0)

我解决了我的问题。没有在Dataset Designer页面上添加查询。我创建了一个sql存储过程,然后使用了数据集设计器。它解决了我的问题。