通常在SQL Server
Common Table Expression子句中,语句前面有分号,如下所示:
;WITH OrderedOrders AS --semicolon here
(
SELECT SalesOrderID, OrderDate,
ROW_NUMBER() OVER (ORDER BY OrderDate) AS 'RowNumber'
FROM Sales.SalesOrderHeader
)
SELECT *
FROM OrderedOrders
WHERE RowNumber BETWEEN 50 AND 60
为什么?
答案 0 :(得分:71)
..FROM..WITH (NOLOCK)..
RESTORE..WITH MOVE..
;
的语句是可选的
放在一起,之前的声明必须在WITH / CTE之前终止。为避免错误,大多数人使用;WITH
,因为我们不知道CTE之前的内容
所以
DECLARE @foo int;
WITH OrderedOrders AS
(
SELECT SalesOrderID, OrderDate,
...;
与
相同DECLARE @foo int
;WITH OrderedOrders AS
(
SELECT SalesOrderID, OrderDate,
...;
MERGE命令有类似的要求。