如何创建表值函数

时间:2016-04-22 12:32:21

标签: sql sql-server

我试图从以下查询创建一个视图,该视图给我结果但我无法保存视图,收到错误

  

关键字'声明'。

附近的语法不正确

后者发现由于查询中的变量,我无法进行查看。有人会帮助我从下面的查询中创建表值函数,请注意我在SQL中很容易使用。

非常感谢提前

DECLARE @NextDate DateTime
SET @NextDate =
(SELECT TOP 1 att.attend_date
FROM Attendance att
WHERE att.ID_Employee=att.ID_Employee and
att.attend_date > att.attend_date
ORDER BY att.attend_date ASC) 
SELECT emp.*,
@NextDate AS NextDate,
   COALESCE(@NextDate, GETDATE()) AS Next123,
   DATEDIFF(DAY, COALESCE(@NextDate, GETDATE()), att.Attend_Date) AS DifferenceDays,
   CASE WHEN ((att.attend_date + 90) < COALESCE(@NextDate, GETDATE())) THEN 1
   ELSE 0
   END AS Day90Credit,
   CASE   
WHEN ((att.Attend_Date + 90) < COALESCE(@NextDate, GETDATE())) THEN        att.Attend_Date + 90
   ELSE att.Attend_Date + 365
   END AS CreditDate,
       att.attend_date,
       att.attend_date +90,
   CASE WHEN (
   CASE   
   WHEN ((att.attend_date + 90) < COALESCE(@NextDate, GETDATE())) THEN 1
   ELSE 0
   END) = 0 AND (              
CASE WHEN ((att.Attend_Date + 90) < COALESCE(@NextDate, GETDATE())) THEN     att.Attend_Date + 90
   ELSE att.Attend_Date + 365
   END) < CURRENT_TIMESTAMP OR (                   
   CASE   
   WHEN ((att.attend_date + 90) < COALESCE(@NextDate, GETDATE())) THEN 1
   ELSE 0
   END) = 1 THEN 1
   ELSE   0
   END AS TotalCredit
   FROM
   attendance att
          INNER JOIN employees emp ON att.ID_Employee= emp.Employee_ID

0 个答案:

没有答案