开发者

What is wrong in the below cte

I have

;with cte as
(
select rn=1, name = CAST('name'as varchar(50))
union all
select rn+1, CAST(name as varchar(50))+ CAST( (rn+1) as varchar(50))
from开发者_如何学Go cte where rn<100)

select * from cte

error

Msg 240, Level 16, State 1, Line 1 Types don't match between the anchor and the recursive part in column "name" of recursive query "cte".


Try something like

;with cte as
(
select  rn=1, 
        name = CAST('name'as varchar(100))
union all
select  rn+1, 
        CAST(name as varchar(50))+ CAST( (rn+1) as varchar(50))
from cte where rn<100)

select * from cte

You have to remember that all fields in the anchor and the recursive part have to be of the same type. That also goes for varchar fields.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜