我正在执行以下语句:
;WITH cte AS (
SELECT
1 as rn,
'name1' as nm
UNION ALL
SELECT
rn + 1,
nm = 'name' + CAST((rn + 1) as varchar(255))
FROM cte a WHERE rn < 10)
SELECT *
FROM cte
...以错误结束...
...which finishes with the error...
Msg 240, Level 16, State 1, Line 2
Types don't match between the anchor and the recursive part in column "nm" of recursive query "cte".
我哪里出错了?
正是它所说的:
'name1'
具有与 'name' + CAST((rn+1) as varchar(255)) 不同的数据类型
试试这个(未经测试)
;with cte as
(
select 1 as rn, CAST('name1' as varchar(259)) as nm
union all
select rn+1,nm = 'name' + CAST((rn+1) as varchar(255))
from cte a where rn<10)
select * from cte
基本上,您也必须确保长度匹配.对于递归位,如果再次失败,您可能必须使用 CAST('name' AS varchar(4))
Basically, you have to ensure the length matches too. For the recursive bit, you may have to use CAST('name' AS varchar(4))
if it fails again
这篇关于CTE 错误:“锚点和递归部分之间的类型不匹配";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!