Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi People.
Well, Recently i Made a function in T-SQL SERVER that the main function is Identify if a secuence of numbers has "Jumps" in example

1,2,3,4,5,6,8,10

with the secuence listed Above the T-SQL funcion show me this result:
"BRINCO DE:1 Folio(s), Del Folio:6, Al folio:8"
"BRINCO DE:1 Folio(s), Del Folio:8, Al folio:10"


but the result is O.k... but the Process time is too long

The mount of numbers to campare is : 156,546
and I spend almost 15 minutes of process
there's another process that you can share with me...but with less time

Thanks in advance
Best regards

EDITED

Sorry here is the function that i made
the records are 156,546

SQL
DECLARE @FolioActual As int
DECLARE @FolioAnterior As Int
DECLARE @iTotal As int
DECLARE @iCant As int
SET @iCant =0

SELECT @iTotal=count(1)
FROM PolizasWebServiceC
WHERE Prefijo ='ACR'
AND Estatus=1

SELECT Prefijo,Folio
INTO #FoliosAscendente
FROM PolizasWebServiceC
WHERE Prefijo ='ACR'
AND Estatus=1
ORDER BY Folio ASC
DELETE FROM Brinco_Folios_DAVID
SET @FolioAnterior=0
SET @FolioActual=0
WHILE @iCant<=@iTotal BEGIN
    IF (@FolioActual-(@FolioAnterior+1))<>0 BEGIN
        INSERT INTO Brinco_Folios_DAVID(Prefijo,FoliosFaltantes,Descripcion,FecReg) VALUES('ACR',(@FolioActual-@FolioAnterior)-1,'Del Folio:' + CAST(@FolioAnterior as varchar(30)) + ', Al folio:' + CAST (@FolioActual as varchar(30)),GetDate() )
    END
    SET @FolioAnterior=@FolioActual
    SELECT TOP 1 @FolioActual=Folio FROM #FoliosAscendente
    DELETE FROM #FoliosAscendente WHERE Folio=@FolioActual
    SET @iCant=@iCant+1
END
Posted
Updated 23-Nov-11 5:50am
v2
Comments
Mehdi Gholam 23-Nov-11 10:54am    
Show the code you are using.
Rene Bustos 23-Nov-11 11:52am    
ready, im posted teh code :D
Mehdi Gholam 23-Nov-11 11:32am    
It should only take a couple of seconds.
Rene Bustos 23-Nov-11 11:52am    
really?
i hope that
Thanks
Amir Mahfoozi 23-Nov-11 11:37am    
Please provide your code.

1 solution

how are you comparing? 15 minutes?

here, try sth. like this.
runs about 1 ms.



SQL
declare @nums varchar(100) = '1,2,3,4,5,6,8,10'

-- first parse the csv string into a temp table
declare @id varchar(20), @start bigint = 0, @end bigint = 0, @rest varchar(max), @sep char(1) = ',';


declare @tabNums table (Num int primary key clustered not null)

	
while 1 = 1
begin	
	set @start = @end + 1;
	set @end = charindex(@sep, @nums, @start)
		
	if @end = 0
		set @id = substring(@nums, @start, len(@nums) - @start + 1);
	else			
		set @id = substring(@nums, @start, @end - @start);
		
	insert
		@tabNums
		(Num)
	values
		(@id)
	;
		
	if @end = 0
		break;
end


-- now check for holes in the chain
declare @num int, @num_before int = -1

declare curNumChain  cursor
	for
		select 
			Num
		from 
			@tabNums
		order by
			Num asc	

open curNumChain ;

Fetch NEXT FROM curNumChain  INTO @num
While (@@FETCH_STATUS <> -1)
begin
	if @num is null
		break;
	
	if @num_before > -1	
		if (@num - @num_before) > 1
		begin
			set @num_before = @num_before + 1
			while @num_before < @num
			begin
				select convert(varchar, @num_before) + ' is missing'
				set @num_before = @num_before + 1
			end
		end
	set @num_before = @num
	Fetch NEXT FROM curNumChain  INTO @num
end
close curNumChain 
deallocate curNumChain 
 
Share this answer
 
Comments
Rene Bustos 23-Nov-11 12:00pm    
O.k i see, but if i want use SELECT numbers FROM TableNumbersToCompare instead declare @nums varchar(100) = '1,2,3,4,5,6,8,10'
Or how can i SET the result of a Query into @nums?
Regards :D
Heino Zunzer 23-Nov-11 12:22pm    
even easier. the @nums part is only to get the things into a temporary table. if it already is a table or a query result, forget the first part.

declare @tabNums table (Num int primary key clustered not null)

insert @tabNums (Num)
select yourNumColumn
from yourTable;

and then go on with the cursor (from -- now check for holes in the chain)
Rene Bustos 23-Nov-11 13:47pm    
Heino, Good and Bad News
Good News.. with your Function i can know exactly what number is not in the secuence, but the time is to long even more that my function, yor function still running so far: 21:34 minutes and seconds still running.
i saw in the data a number : 233454 and the next is 322993 and ther a lot of rows generated by this hole, but course i gonna give you an accepted solution
thanks
Best regards
:D
Rene Bustos 23-Nov-11 13:43pm    
ok let me Check then Thanks Heino i'll be back as soon as possible with the results

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900