我想为chars [a-z]创建一个小cte。 但是,如果我使用char() - 函数作为递归部分,如下所示:
with cte(chars)
as (
select cast('a' as char) as chars
union all
select char(ascii(chars) + 1 ) from cte
where chars < 'z'
)
select *
from cte
数据类型不匹配,即使char()返回char(1)。 但是如果char() - 函数被转换为char(1),它运行得很好:
with cte(chars)
as (
select cast('a' as char) as chars
union all
select cast(char(ascii(chars) + 1 ) as char) from cte
where chars < 'z'
)
select *
from cte
我错过了什么吗?
答案 0 :(得分:1)
在SQL Server中,从不使用char()
(和相关类型)而没有长度。默认长度因环境而异 - 谁想跟踪这些事情?
以下作品:
with cte(chars) as (
select cast('a' as char(1)) as chars
union all
select char(ascii(chars) + 1 ) from cte
where chars < 'z'
)
select *
from cte;
换句话说,char()
中cast()
的默认长度与declare
中的默认长度不同。