1
I have a database created by apróx. a decade, this same bank has several records and obviously, by being in production, can not be changed from day to night.
The problem is:
The products registered in the bank have serials, these serials at the beginning of everything there was no need to record the check digit, however, nowadays the digit is necessary to generate reports and everything else.
To calculate this digit makes up module 10 with the 19 serial numbers, to perform this calculation, we have a function of the SQL server that initiates a while
to break and add the serial digits. Other while
to perform the necessary calculations and then a if
, to return the equivalent number in delta.
Below the function:
DECLARE @delta AS TABLE ( indice INT, valor INT )
INSERT INTO @delta
( indice, valor )
VALUES ( 0, 0 ),
( 1, 1 ),
( 2, 2 ),
( 3, 3 ),
( 4, 4 ),
( 5, -4 ),
( 6, -3 ),
( 7, -2 ),
( 8, -1 ),
( 9, 0 )
DECLARE @serie VARCHAR(50) = '############'
DECLARE @deltaindex INT
DECLARE @deltavalue INT
DECLARE @soma INT = 0
DECLARE @i INT = 1
WHILE ( @i <= LEN(@serie) )
BEGIN
SET @soma = @soma + CAST(SUBSTRING(@serie, @i, 1) AS INT)
SET @i = @i + 1
END
SET @i = LEN(@serie)
WHILE ( @i > 0 )
BEGIN
SET @deltaindex = CAST(SUBSTRING(@serie, @i, 1) AS INT)
SELECT @deltavalue = valor
FROM @delta
WHERE indice = @deltaindex
SET @soma = @soma + @deltavalue
SET @i = @i - 2
END
DECLARE @mod10 INT
SET @mod10 = @soma % 10
SET @mod10 = 10 - @mod10
IF ( @mod10 = 10 )
BEGIN
SET @mod10 = 0
END
PRINT @mod10
That being said, we have approx. 1.5KK of records that need to be displayed in a report and each record has between 1 and 2 serials and this per serial query ends up delaying quite the display of the report, enough so that I can not issue a report by the system much less by the bank, taking around 1H30 to issue and even so I had to cancel the query.
I wonder if there is any way to optimize the module of 10 so that the consultation does not delay so much.
The serial always has 19 digits?
– José Diz
No, it varies between 5 and 49
– Márcio Eric