我的代码
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;)附近的语法错误。
。任何想法?
答案 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 BY
和TOP 100
完全是多余的。 SQL Server不保证结果是有序的。