Sql server CTE如何在sql内部工作?
我在跟踪执行以下代码的顺序时遇到问题: 代码工作正常 我只是想知道怎么做Sql server CTE如何在sql内部工作?,sql-server,common-table-expression,Sql Server,Common Table Expression,我在跟踪执行以下代码的顺序时遇到问题: 代码工作正常 我只是想知道怎么做 with MyCTE(x) as ( 1) select x = convert(varchar(8000),'hello') // line 1 union all 3) select x + 'a' from MyCTE where len(x) < 100 //line 3 ) select x from MyCTE order by x 与
with MyCTE(x)
as
(
1) select x = convert(varchar(8000),'hello') // line 1
union all
3) select x + 'a' from MyCTE where len(x) < 100 //line 3
)
select x from MyCTE
order by x
与MyCTE(x)
作为
(
1) 选择x=convert(varchar(8000),'hello')//第1行
联合所有
3) 从MyCTE中选择x+‘a’,其中len(x)<100//第3行
)
从MyCTE中选择x
按x订购
MSDN:
递归执行的语义如下所示:
将CTE表达式拆分为锚定成员和递归成员
运行锚定成员以创建第一个调用或基本结果
设置(T0)
运行递归成员,将Ti作为输入,Ti+1作为输出
重复步骤3,直到返回空集合
返回结果集。这是一个从T0到Tn的联盟
阶段:
1) 执行第1行(x=hello)
2) 执行第3行(helloa)
3) 现在它这样称呼自己:这里x又回到了hello!!(第1行)
- 根据:line1,每当cte调用自身时,x始终应重置!(或者T0是否在递归中被绕过?)
- (x)部分的角色是什么?MyCTE(x)?输入还是输出
据我所知,(x)是输出值,而不是输入。T0/Line1作为锚执行一次
with MyCTE(x)
as
(
select x = convert(varchar(8000),'hello') -- Anchor, executed once
union all
select x + 'a' from MyCTE where len(x) < 100 -- Recursion, executed n times
)
select x from MyCTE order by x
T0/Line1作为锚执行一次
with MyCTE(x)
as
(
select x = convert(varchar(8000),'hello') -- Anchor, executed once
union all
select x + 'a' from MyCTE where len(x) < 100 -- Recursion, executed n times
)
select x from MyCTE order by x
这是一个理论问题,还是你运行过这个?@gbn-嗨,我已经运行了它,但我不知道如何运行。-
每当cte调用自身时-x总是应该重置
,但它没有重置(这很好),但我不知道为什么:每次它都会说:选择x=convert(varchar(8000),'hello')
这是一个理论问题,还是你运行过这个?@gbn-嗨,我已经运行了它,但我不知道如何运行。-每当cte调用自身时-x总是应该重置
,但它没有重置(这很好),但我不知道为什么:它每次都说:选择x=convert(varchar(8000),'hello')
一如既往-你很棒。谢谢问题是,我认为第1行总是得到执行……一如既往——你很棒。谢谢问题是我认为行1
总是得到执行。。。。