在浏览SQL CTE时,我遇到了这个问题:CODE Project CTE
代码是:
WITH ShowMessage(STATEMENT, LENGTH)
AS
(
SELECT STATEMENT = CAST('I Like ' AS VARCHAR(300)), LEN('I Like ')
UNION ALL
SELECT
CAST(STATEMENT + 'CodeProject! ' AS VARCHAR(300))
, LEN(STATEMENT) FROM ShowMessage
WHERE LENGTH < 300
)
SELECT STATEMENT, LENGTH FROM ShowMessage
甚至是一个小修改过的:
WITH ShowMessage(STATEMENT, LENGTH)
AS
(
SELECT STATEMENT = 1, LEN('I Like ')
UNION ALL
SELECT
STATEMENT + 1
, LEN(STATEMENT) FROM ShowMessage
WHERE STATEMENT < 50
)
SELECT STATEMENT, LENGTH FROM ShowMessage
当我尝试代码时,上面的代码非常完美:
with k (TT,LL)
as
(
select TT= 1, 1
union all
select TT+1,1
WHERE TT < 50
)
select TT,LL from k
我的代码不起作用,错误是COLUMN TT不存在。仔细观察后发现STATEMENT是一个关键字(UI显示为蓝色);然后我开始在网上搜索这个关键字的含义,但找不到一个(Google总是只抛出SELECT语句 - 而不是STATEMENT)
请您解释一下这个STATEMENT关键字是什么以及在何处/如何使用它。或者请指出正确的来源来学习它。
答案 0 :(得分:1)
尝试以下查询:
;WITH k (TT,LL)
as
(
SELECT 1, 1
UNION ALL
SELECT TT+1,1
FROM k --- you miss that table
WHERE TT < 50
)
SELECT TT,LL FROM k
答案 1 :(得分:0)
你错过了添加表格,即
with k (TT,LL)
as
(
select TT=1 , 1
union all
select TT+1,1 From K
WHERE TT < 50
)
select TT,LL from k