N(N)在此公用表表达式中的含义是什么

时间:2018-08-24 07:50:10

标签: sql sql-server tsql

我看到了这个csv解析器函数here,我想知道cte1中N(N)是什么意思

CREATE FUNCTION [dbo].[udfParse] (@String varchar(max))
Returns Table 
As

Return (  

    with   cte1(N)   As (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)),
           cte2(N)   As (Select Top (IsNull(DataLength(@String),0)) Row_Number() over (Order By (Select NULL)) From (Select N=1 From cte1 a,cte1 b,cte1 c,cte1 d) A ),
           cte3(N)   As (Select 1 Union All Select t.N+1 From cte2 t Where Substring(@String,t.N,1) = ','),
           cte4(N,L) As (Select S.N,IsNull(NullIf(CharIndex(',',@String,s.N),0)-S.N,8000) From cte3 S)

    Select RetSeq = Row_Number() over (Order By A.N)
          ,RetVal = LTrim(RTrim(Substring(@String, A.N, A.L)))
    From   cte4 A where LTrim(RTrim(Substring(@String, A.N, A.L))) <> ''
);

2 个答案:

答案 0 :(得分:1)

您也可以写A(B)A是子查询的别名,B是子查询(Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1))返回的列的别名。

答案 1 :(得分:1)

from子句中的values语句创建一个没有名称且其列没有名称的派生表。

as N(N)给他们起了名字,N代表表,N代表列。

这些都是等效的:

select n.n from (Values (1), (1)) N(N)

select 1 from (Values (1), (1)) N(N)

select 1 as n from (Values (1), (1)) a(b)

select a.b as n from (Values (1), (1)) a(b)