Sql Server:使用外部表进行cte查询

时间:2014-05-08 09:23:25

标签: sql-server-2012

我有这段代码:

with cte as
(select m.ID , 1 i from MyTable m union all
 select ID  , cte.i +1 i from cte  where i < 5)
select * from cte

错误讯息: Msg 319,Level 15,State 1,Line 2 关键字'with'附近的语法不正确。如果此语句是公用表表达式,xmlnamespaces子句或更改跟踪上下文子句,则必须以分号结束前一个语句。

有什么问题?

1 个答案:

答案 0 :(得分:0)

declare @MyTable table (id int)
insert @MyTable values (1),(2),(3),(4),(5);

with cte(id,i) as
(select m.ID , 1 from @MyTable m union all
 select ID  , cte.i +1  from cte  where cte.i < 5)
select * from cte