Common Table Expression, why semicolon?(公用表表达式,为什么要分号?)
问题描述
通常在SQL Server Common Table Expression 子句中,语句前有分号,像这样:
Usually in SQL Server Common Table Expression clause there is semicolon in front of the statement, like this:
;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
为什么?
推荐答案
- 为了避免歧义,因为 WITH 可以在别处使用
..FROM..WITH (NOLOCK)..
RESTORE..WITH MOVE.. - 在 SQL Server 中使用
;终止语句是可选的 - To avoid ambiguity because WITH can be used elsewhere
..FROM..WITH (NOLOCK)..
RESTORE..WITH MOVE.. - It's optional to terminate statements with
;in SQL Server
总而言之,前面的语句必须在 WITH/CTE 之前终止.为了避免错误,大多数人使用 ;WITH 因为我们不知道 CTE 之前是什么
Put together, the previous statement must be terminated before a WITH/CTE. To avoid errors, most folk use ;WITH because we don't know what is before the CTE
所以
DECLARE @foo int;
WITH OrderedOrders AS
(
SELECT SalesOrderID, OrderDate,
...;
与
DECLARE @foo int
;WITH OrderedOrders AS
(
SELECT SalesOrderID, OrderDate,
...;
MERGE 命令有类似的要求.
The MERGE command has a similar requirement.
这篇关于公用表表达式,为什么要分号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:公用表表达式,为什么要分号?
基础教程推荐
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 无法解决整理冲突 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
