gpt4 book ai didi

sql - CTE错误: "Types don' t match between the anchor and the recursive part"

转载 作者:行者123 更新时间:2023-12-01 17:44:36 25 4
gpt4 key购买 nike

我正在执行以下语句:

;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

...最终出现错误...

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))

关于sql - CTE错误: "Types don' t match between the anchor and the recursive part",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1838276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com