我需要在列" NumberList "列中插入位于“开始”和“结束”列之间的数字。 (这应该包括开始和结束数字。)
我用来获得上述结果的查询如下
SELECT DISTINCT Number
, CONVERT(VARCHAR,Number) + REPLICATE('0',16-LEN(Number)) AS 'Start'
, CONVERT(VARCHAR,Number) + REPLICATE('9',16-LEN(Number)) AS 'End'
,NULL 'NumberList'
FROM Table
请您帮我解决以下问题,因为我希望 NumberList 列也是主键
答案 0 :(得分:0)
Create function GetNumbersList (@intStart as bigint, @intEnd as bigint)
returns varchar(max)
as
begin
DECLARE @num INT = @intStart
DECLARE @numberlist as varchar(max)=''
WHILE(@num<=@intEnd)
begin
set @numberlist=@numberlist + cast(@num as varchar(20)) + N' '
SET @num = @num + 1
end
return(rtrim(@numberlist))
End
GO
select Number
, CONVERT(VARCHAR,Number) + REPLICATE('0',16-LEN(Number)) AS 'Start'
, CONVERT(VARCHAR,Number) + REPLICATE('9',16-LEN(Number)) AS 'End'
, dbo.GetNumbersList (CONVERT(VARCHAR,Number) + REPLICATE('0',16-LEN(Number)), CONVERT(VARCHAR,Number) + REPLICATE('9',16-LEN(Number))) as [NumbersList]
from Table