簡體   English   中英

T-SQL CTE char()函數的類型與cast(as char)的類型不同

[英]T-SQL CTE char() function not the same type as cast( as char)

我想為字符[az]創建一個小的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

我想念什么嗎?

在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;

換句話說, cast() char()的默認長度與declare的默認長度不同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM