不支持Declare SQL构造或语句

时间:2015-01-16 07:03:05

标签: sql-server tsql

使用显示的查询时,出现错误。

  

不支持Declare SQL构造或语句。

帮我找到合适的解决方案。

DECLARE @maxcount1 float 
DECLARE @maxcount0 float 
DECLARE @start datetime 
SET @start = getdate() - 2 
DECLARE @end datetime 
SET @end= getdate() + 2

SELECT @maxcount1 = max(OnTimeCount),@maxcount0 = max(DueTimeCount)

FROM  (SELECT DISTINCT COUNT(DISTINCT CASE WHEN ontime = 1 THEN sl_no  END)  AS OnTimeCount,  COUNT(DISTINCT CASE WHEN ontime = 0 THEN  sl_no END) AS DueTimeCount FROM tbl_splited_details 

WHERE (uploaded_date  BETWEEN  @start  AND @end) GROUP BY Site_ID) AS s

SELECT  DISTINCT Site_ID, OnTimeCount, DueTimeCount, 
round((cast((OnTimeCount*100) AS float)/@maxcount1), 2)AS percentage1 , round((cast((DueTimeCount *100)AS float)/@maxcount1),  2) AS percentage0

FROM (SELECT DISTINCT  Site_ID, COUNT(DISTINCT CASE  WHEN ontime  =   1  THEN  sl_no  END)  AS OnTimeCount, 
COUNT(DISTINCT CASE WHEN ontime  =  0 THEN sl_no END) AS  DueTimeCount  

FROM  tbl_splited_details  WHERE (uploaded_date BETWEEN  @start  AND    @end)   GROUP BY    Site_ID) AS s                                    

1 个答案:

答案 0 :(得分:3)

据我所知,您使用以下语法:

DECLARE @start datetime = getdate() - 2 

仅在SQL Server 2008及更高版本中支持此DECLARE语法。 在使用SQL Server 2005的情况下,必须使用较旧的语法:

DECLARE @start datetime
SET @start = getdate() - 2