Atribuir números sequenciais dentro de um intervalo de valores em T-SQL

Para gerar identificadores:

DECLARE @Demoid INT = - 1;

DECLARE @Recordcount INT = 1;
DECLARE @TotalRecords INT = 0;
DECLARE @Min BIGINT, @Max BIGINT;

SELECT @Min = 100000, @Max = 10000000;

SELECT TOP (@Max - @Min + 1) @Min - 1 + ROW NUMBER() OVER (ORDER BY t1.number) AS N
INTO #SequenceNumbersWinthinRange
FROM MASTER..spt_values t1
CROSS JOIN MASTER..spt_values t2;

CREATE TABLE #TableRows AS (ID INT IDENTITY(l,1) NOT NULL, DemoId INT NOT NULL);

INSERT INTO #TableRows (DemoId)
SELECT ID FROM demo;

SET @TotalRecords = @@rowcount;

WHILE @TotalRecords >= @RecordCount
BEGIN

SELECT @Demoid = DemoId FROM #TableRows WHERE ID = @RecordCount;

UPDATE demo
SET Name =(SELECT MIN(N) FROM #SequenceNumbersWinthinRange WHERE N NOT IN (SELECT Name FROM Demo))
WHERE ID = @Demoid;

SELECT @RecordCount = @RecordCount + 1, @Demoid = -1;
END ;


fonte: https://stackoverflow.com/questions/21425546/how-to-generate-a-range-of-numbers-between-two-numbers

Licença CC BY-SA 4.0 Silvia Pinhão Lopes, 29.3.22
Print Friendly and PDF

Sem comentários:

Com tecnologia do Blogger.