问题描述
嗨伙计!!
我对CTE有疑问..如果我们在SP中使用CTE。以前的语句必须以Semicolon(;)终止..为什么?
声明@date varchar(10)
set @ date = GETDATE();
$ / b $ b与CTEQuery AS(
SELECT CAST('2014年3月23日'作为DATETIME)AS dt
UNION ALL
SELECT DATEADD(dd,1,dt)
来自CTEQuery s
WHERE DATEADD(dd,1,dt)< = CAST('2014年6月26日'作为日期)
)
从CTEQuery中选择*
谢谢提前!!
我尝试了什么:
我不喜欢知道为什么需要Semicolon。我只是想知道原因。
Hi Folks!!
I have a doubt on CTE..If we are using CTE inside SP..Previous statement must be terminated with Semicolon(;)..Why??
declare @date varchar(10)
set @date=GETDATE();
WITH CTEQuery AS (
SELECT CAST('23 Mar 2014' AS DATETIME) AS dt
UNION ALL
SELECT DATEADD(dd, 1, dt)
FROM CTEQuery s
WHERE DATEADD(dd, 1, dt) <= CAST('26 Jun 2014' AS DATETIME)
)
select * from CTEQuery
Thanks in Advance!!
What I have tried:
I don't know the reason why Semicolon is required .I just want to know the Reason .
推荐答案
当CTE用于作为批处理一部分的语句中时,状态nt之前必须跟一个分号。
When a CTE is used in a statement that is part of a batch, the statement before it must be followed by a semicolon.
你必须阅读 [仔细。
我猜他们需要分号来正确解析语句,因为WITH也可用于其他事情,例如选择提示:
You must read the documentation[^] carefully.
I guess they needed that semicolon to parse the statement correctly as WITH can be used for other things too e.g. hints in selects:
SELECT * FROM MyTable WITH (NOLOCK)
或某些命令中的选项:
or options in some commands:
ALTER TABLE MyTable REBUILD WITH (DATA_COMPRESSION = PAGE).
类似于 []命令必须始终以分号结束。
It is similar to MERGE[^] command which must always be terminated by a semicolon.
这篇关于Cte用分号问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!