我正在尝试用数字0900000000000000000000000007165中的字段填写表中的一列到数字0900000000000000000000000000000008165
我有一个问题,对于这段代码来说,这些数字太大了,我不知道该怎么办
类型:ID-varchar(31)
错误:(翻译)
消息240,第16级,状态1,第3行在循环“ cter”查询的“开始”列中,锚点与循环部分之间的类型不匹配。
with cter as (
select 0900000000000000000000000007165 as start, 0900000000000000000000000008165 as loop_end
union all
select c.start + 1, loop_end
from cter c
where c.start < loop_end
)
INSERT INTO [Base].[dbo].[Table] (Id)
select c.start
from cter c
option (maxrecursion 0);
您将获得算术溢出错误,因为即使对于bigint数据类型,此值也太大。
我建议您将其视为字符并删除前缀,然后再添加前缀,如@ Tyron78所建议的
DECLARE @loop_start CHAR(31) = '0900000000000000000000000007165'
, @loop_end CHAR(31) = '0900000000000000000000000008165'
;with cter as
(
select CAST(RIGHT(@loop_start,4) as int) as start, CAST(RIGHT(@loop_end,4) AS INT) loop_end
union all
select c.start + 1, loop_end
from cter c
where c.start < loop_end
)
select CONCAT(LEFT(@loop_start,27), c.start)
from cter c
option (maxrecursion 0);
结果集是:
+---------------------------------+
| 0900000000000000000000000007165 |
| 0900000000000000000000000007166 |
| . |
| . |
| . |
| 0900000000000000000000000008163 |
| 0900000000000000000000000008164 |
| 0900000000000000000000000008165 |
+---------------------------------+