')附近的语法不正确... ...我不明白

时间:2017-05-09 12:03:49

标签: sql syntax

我的代码

RETURNS TABLE 
AS
RETURN 
(
with Documents as
(
select TOP 100 PERCENT d.LeEn, d.Wacode, d.Waname, d.Arcode, d.Arname, sum(d.Quantity) as Quantity from Documents as d
where (select max(DocumentDate) from Documents as d where d.DocumentCode = 'INW') <= d.DocumentDate
group by d.LeEn, d.WarehouseCode, d.Waname, d.Arcode, d.Arname
order by d.LeEn, d.WarehouseCode, d.Arcode
)
)

我正在接受这个

&#39;)附近的语法错误。

任何想法?

1 个答案:

答案 0 :(得分:3)

WITH应该在SELECT之前。在这种情况下,CTE似乎没必要,所以只需:

RETURN (
 select TOP 100 PERCENT d.LeEn, d.Wacode, d.Waname, d.Arcode, d.Arname, sum(d.Quantity) as Quantity
 from Documents as d
 where (select max(DocumentDate)
        from Documents d
        where d.DocumentCode = 'INW'
       ) <= d.DocumentDate
 group by d.LeEn, d.WarehouseCode, d.Waname, d.Arcode, d.Arname
 order by d.LeEn, d.WarehouseCode, d.Arcode
)

ORDER BYTOP 100完全是多余的。 SQL Server不保证结果是有序的。