Guys,
Good morning.
Today I'm going to make a quick post for you that is very useful in everyday life, especially for those who create integration routines with Sefaz, Revenue and other government bodies and need a routine to validate state registration for all states in Brazil, or even for those who want to have this field in their system with validated and intact data.
To carry out this check, I found a very complete function on a blog, developed by Rodrigo Ebner, which fulfills this need well.
Examples of use
Let's see how we use this function to validate state registration:
-- Validando inscrição estadual do Pará
SELECT dbo.fncValida_Inscricao_Estadual_Geral('PA', '596142080')
SELECT dbo.fncValida_Inscricao_Estadual_Geral('PA', '586142080')
-- Validando inscrição estadual do Espírito Santo
SELECT dbo.fncValida_Inscricao_Estadual_Geral('ES', '355206196')
SELECT dbo.fncValida_Inscricao_Estadual_Geral('ES', '355206197')
-- Validando inscrição estadual de São Paulo
SELECT dbo.fncValida_Inscricao_Estadual_Geral('SP', '556261170177')
SELECT dbo.fncValida_Inscricao_Estadual_Geral('SP', '556261170177')
Function source code
Now that we've seen how this UDF (User Defined Function) is used, let's take a look at its source code to understand how numbers are validated and you can create this function in your environment too:
View source codeCREATE FUNCTION [dbo].[fncValida_Inscricao_Estadual_Geral]
(
@Ds_UF varchar(2),
@Ds_Inscricao_Estadual varchar(18)
)
RETURNS BIT
BEGIN
DECLARE @counter INT;
DECLARE @b INT;
DECLARE @soma INT;
DECLARE @dig INT;
--auxiliares
DECLARE @p INT;
DECLARE @d INT;
DECLARE @i VARCHAR(18);
DECLARE @die VARCHAR(13);
IF LEN(@Ds_Inscricao_Estadual) > 0 AND LEN(@Ds_UF) = 2
BEGIN
--retira caracteres especiais da IE
SET @Ds_Inscricao_Estadual = REPLACE(REPLACE(REPLACE(LTRIM(RTRIM(@Ds_Inscricao_Estadual)), '.', ''), '-', ''), '/', '');
-- verifica IE para o estado AC
IF @Ds_UF = 'AC'
BEGIN
--verifica tamanho da IE
IF LEN(@Ds_Inscricao_Estadual) <> 13 OR SUBSTRING(@Ds_Inscricao_Estadual, 1, 2) <> '01'
RETURN(0);
--calcula primeiro digito verificador
SET @counter = 1;
SET @b = 4;
SET @soma = 0;
WHILE @counter < 12
BEGIN
SET @soma = @soma + ( SUBSTRING(@Ds_Inscricao_Estadual, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b - 1;
IF @b = 1
SET @b = 9;
END
SET @dig = 11 - ( @soma % 11 );
IF @dig >= 10
SET @dig = 0;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 12, 1)
RETURN (0);
--calcula segundo digito verificador
SET @counter = 1;
SET @b = 5;
SET @soma = 0;
WHILE @counter < 13
BEGIN
SET @soma = @soma + ( SUBSTRING(@Ds_Inscricao_Estadual, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b - 1;
IF @b = 1
SET @b = 9;
END
SET @dig = 11 - ( @soma % 11 );
IF @dig >= 10
SET @dig = 0;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 13, 1)
RETURN (0);
END
--AC
-- verifica IE para o estado AL
ELSE IF @Ds_UF = 'AL'
BEGIN
--verifica tamanho da IE
IF LEN(@Ds_Inscricao_Estadual) <> 9 OR SUBSTRING(@Ds_Inscricao_Estadual, 1, 2) <> '24'
RETURN(0);
--calcula primeiro digito verificador
SET @counter = 1;
SET @b = 9;
SET @soma = 0;
WHILE (@counter < 9)
BEGIN
SET @soma = @soma + ( SUBSTRING(@Ds_Inscricao_Estadual, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b - 1;
END
SET @soma = @soma * 10;
SET @dig = @soma - FLOOR(@soma / 11) * 11;
IF (@dig = 10)
SET @dig = 0;
IF (@dig <> SUBSTRING(@Ds_Inscricao_Estadual, 9, 1))
RETURN (0);
END
--AL
-- verifica IE para o estado AM
ELSE IF @Ds_UF = 'AM'
BEGIN
--verifica tamanho da IE
IF LEN(@Ds_Inscricao_Estadual) <> 9
RETURN(0);
--calcula primeiro digito verificador
SET @counter = 1;
SET @b = 9;
SET @soma = 0;
WHILE @counter < 9
BEGIN
SET @soma = @soma + ( SUBSTRING(@Ds_Inscricao_Estadual, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b - 1;
END
IF @soma < 11
SET @dig = 11 - @soma;
ELSE
IF @soma % 11 <= 1
SET @dig = 0;
ELSE
SET @dig = 11 - ( @soma % 11 );
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 9, 1)
RETURN (0);
END
--AM
-- verifica IE para o estado AP
ELSE IF @Ds_UF = 'AP'
BEGIN
--verifica tamanho da IE
IF LEN(@Ds_Inscricao_Estadual) <> 9
RETURN(0);
--calcula primeiro digito verificador
SET @p = 0;
SET @d = 0;
SET @i = SUBSTRING(@Ds_Inscricao_Estadual, 1, 8);
IF @i >= 3000001 AND @i <= 3017000
BEGIN
SET @p = 5;
SET @d = 0;
END
ELSE
IF @i >= 3017001 AND @i <= 3019022
BEGIN
SET @p = 9;
SET @d = 1;
END
SET @counter = 1;
SET @b = 9;
SET @soma = @p;
WHILE @counter < 9
BEGIN
SET @soma = @soma + ( SUBSTRING(@i, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b - 1;
END
SET @dig = 11 - ( @soma % 11 );
IF @dig = 10
SET @dig = 0;
ELSE
IF @dig = 11
SET @dig = @d;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 9, 1)
RETURN (0);
END
--AP
-- verifica IE para o estado BA
ELSE IF @Ds_UF = 'BA'
BEGIN
--verifica tamanho da IE
IF LEN(@Ds_Inscricao_Estadual) <> 8
RETURN(0);
--calcula segundo digito verificador
SET @counter = 1;
SET @b = 7;
SET @soma = 0;
WHILE @counter < 7
BEGIN
SET @soma = @soma + ( SUBSTRING(@Ds_Inscricao_Estadual, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b - 1;
END
IF SUBSTRING(@Ds_Inscricao_Estadual, 1, 1) IN ( '0', '1', '2', '3', '4', '5', '8' )
SET @dig = 10 - ( @soma % 10 );
ELSE
BEGIN
SET @dig = 11 - ( @soma % 11 );
IF @dig <= 1
SET @dig = 0;
END
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 8, 1)
RETURN (0);
--calcula primeiro digito verificador
SET @counter = 1;
SET @b = 8;
SET @soma = 0;
WHILE @counter < 9
BEGIN
IF @counter <> 7
BEGIN
SET @soma = @soma + ( SUBSTRING(@Ds_Inscricao_Estadual, @counter, 1) * @b );
SET @b = @b - 1;
END
SET @counter = @counter + 1;
END
IF SUBSTRING(@Ds_Inscricao_Estadual, 1, 1) IN ( '0', '1', '2', '3', '4', '5', '8' )
SET @dig = 10 - ( @soma % 10 );
ELSE
BEGIN
SET @dig = 11 - ( @soma % 11 );
IF @dig <= 1
SET @dig = 0;
END
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 7, 1)
RETURN (0);
END
--BA
-- verifica IE para o estado CE
ELSE IF @Ds_UF = 'CE'
BEGIN
--verifica tamanho da IE
IF LEN(@Ds_Inscricao_Estadual) > 9
RETURN(0);
SET @die = @Ds_Inscricao_Estadual;
IF ( LEN(@Ds_Inscricao_Estadual) < 9 )
BEGIN
WHILE LEN(@die) <= 8
BEGIN
SET @die = '0' + @die;
END
END
--calcula primeiro digito verificador
SET @counter = 1;
SET @b = 9;
SET @soma = 0;
WHILE @counter < 9
BEGIN
SET @soma = @soma + ( SUBSTRING(@die, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b - 1;
END
SET @dig = 11 - ( @soma % 11 );
IF @dig >= 10
SET @dig = 0;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 9, 1)
RETURN (0);
END
--CE
-- verifica IE para o estado DF
ELSE IF @Ds_UF = 'DF'
BEGIN
--verifica tamanho da IE
IF LEN(@Ds_Inscricao_Estadual) <> 13 OR SUBSTRING(@Ds_Inscricao_Estadual, 1, 2) <> '07'
RETURN(0);
--calcula primeiro digito verificador
SET @counter = 1;
SET @b = 4;
SET @soma = 0;
WHILE @counter < 12
BEGIN
SET @soma = @soma + ( SUBSTRING(@Ds_Inscricao_Estadual, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b - 1;
IF @b = 1
SET @b = 9;
END
SET @dig = 11 - ( @soma % 11 );
IF @dig >= 10
SET @dig = 0;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 12, 1)
RETURN (0);
--calcula segundo digito verificador
SET @counter = 1;
SET @b = 5;
SET @soma = 0;
WHILE @counter < 13
BEGIN
SET @soma = @soma + ( SUBSTRING(@Ds_Inscricao_Estadual, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b - 1;
IF @b = 1
SET @b = 9;
END
SET @dig = 11 - ( @soma % 11 );
IF @dig >= 10
SET @dig = 0;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 13, 1)
RETURN (0);
END
--DF
-- verifica IE para o estado ES
ELSE IF @Ds_UF = 'ES'
BEGIN
--verifica tamanho da IE
IF LEN(@Ds_Inscricao_Estadual) <> 9
RETURN(0);
--calcula primeiro digito verificador
SET @counter = 1;
SET @b = 9;
SET @soma = 0;
WHILE @counter < 9
BEGIN
SET @soma = @soma + ( SUBSTRING(@Ds_Inscricao_Estadual, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b - 1;
END
SET @dig = @soma % 11;
IF @dig < 2
SET @dig = 0;
ELSE
SET @dig = 11 - @dig;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 9, 1)
RETURN (0);
END
--ES
-- verifica IE para o estado GO
ELSE IF @Ds_UF = 'GO'
BEGIN
--verifica tamanho da IE
IF LEN(@Ds_Inscricao_Estadual) <> 9 OR SUBSTRING(@Ds_Inscricao_Estadual, 1, 2) NOT IN ( '10', '11', '15' )
RETURN(0);
--calcula primeiro digito verificador
SET @counter = 1;
SET @b = 9;
SET @soma = 0;
WHILE @counter < 9
BEGIN
SET @soma = @soma + ( SUBSTRING(@Ds_Inscricao_Estadual, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b - 1;
END
SET @dig = @soma % 11;
IF ( @dig = 1 )
BEGIN
IF SUBSTRING(@Ds_Inscricao_Estadual, 1, 8) >= 10103105 AND SUBSTRING(@Ds_Inscricao_Estadual, 1, 8) <= 10119997
SET @dig = 1;
ELSE
SET @dig = 0
END
ELSE
IF @dig > 1
SET @dig = 11 - @dig;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 9, 1)
RETURN (0);
END
--GO
-- verifica IE para o estado MA
ELSE IF @Ds_UF = 'MA'
BEGIN
--verifica tamanho da IE
IF LEN(@Ds_Inscricao_Estadual) <> 9 OR SUBSTRING(@Ds_Inscricao_Estadual, 1, 2) <> '12'
RETURN(0);
--calcula primeiro digito verificador
SET @counter = 1;
SET @b = 9;
SET @soma = 0;
WHILE @counter < 9
BEGIN
SET @soma = @soma + ( SUBSTRING(@Ds_Inscricao_Estadual, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b - 1;
END
SET @dig = @soma % 11;
IF @dig <= 1
SET @dig = 0;
ELSE
SET @dig = 11 - @dig;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 9, 1)
RETURN (0);
END
--MA
-- verifica IE para o estado MG
ELSE IF @Ds_UF = 'MG'
BEGIN
IF SUBSTRING(@Ds_Inscricao_Estadual, 1, 2) = 'PR' OR SUBSTRING(@Ds_Inscricao_Estadual, 1, 5) = 'ISENT'
RETURN(1);
--verifica tamanho da IE
IF LEN(@Ds_Inscricao_Estadual) <> 13
RETURN(0);
SET @die = SUBSTRING(@Ds_Inscricao_Estadual, 1, 3) + '0' + SUBSTRING(@Ds_Inscricao_Estadual, 4, 11);
--calcula primeiro digito verificador
SET @counter = 1;
SET @b = 1;
SET @soma = 0;
WHILE @counter < 13
BEGIN
IF ( SUBSTRING(@die, @counter, 1) * @b ) >= 10
SET @soma = @soma + ( ( SUBSTRING(@die, @counter, 1) * @b ) - 9 );
ELSE
SET @soma = @soma + ( SUBSTRING(@die, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b + 1;
IF @b = 3
SET @b = 1;
END
SET @dig = ( ( FLOOR(@soma / 10) + 1 ) * 10 ) - @soma;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 12, 1)
RETURN (0);
--calcula segundo digito verificador
SET @counter = 1;
SET @b = 3;
SET @soma = 0;
WHILE @counter < 13
BEGIN
SET @soma = @soma + ( SUBSTRING(@Ds_Inscricao_Estadual, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b - 1;
IF @b = 1
SET @b = 11;
END
SET @dig = 11 - ( @soma % 11 );
IF @dig >= 10
SET @dig = 0;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 13, 1)
RETURN (0);
END
--MG
-- verifica IE para o estado MT
ELSE IF @Ds_UF = 'MT'
BEGIN
--verifica tamanho da IE
IF LEN(@Ds_Inscricao_Estadual) < 9
RETURN(0);
SET @die = @Ds_Inscricao_Estadual;
IF ( LEN(@Ds_Inscricao_Estadual) < 11 )
BEGIN
WHILE LEN(@die) <= 11
BEGIN
SET @die = '0' + @die;
END
END
--calcula primeiro digito verificador
SET @counter = 1;
SET @b = 3;
SET @soma = 0;
WHILE @counter < 11
BEGIN
SET @soma = @soma + ( SUBSTRING(@die, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b - 1;
IF @b = 1
SET @b = 9;
END
SET @dig = @soma % 11;
IF @dig <= 1
SET @dig = 0;
ELSE
SET @dig = 11 - @dig;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 11, 1)
RETURN (0);
END
--MT
-- verifica IE para o estado MS
ELSE IF @Ds_UF = 'MS'
BEGIN
--verifica tamanho da IE
IF LEN(@Ds_Inscricao_Estadual) <> 9 OR SUBSTRING(@Ds_Inscricao_Estadual, 1, 2) <> '28'
RETURN(0);
--calcula primeiro digito verificador
SET @counter = 1;
SET @b = 9;
SET @soma = 0;
WHILE @counter < 9
BEGIN
SET @soma = @soma + ( SUBSTRING(@Ds_Inscricao_Estadual, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b - 1;
END
SET @dig = @soma % 11;
IF @dig <= 1
SET @dig = 0;
ELSE
SET @dig = 11 - @dig;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 9, 1)
RETURN (0);
END
--MS
-- verifica IE para o estado PA
ELSE IF @Ds_UF = 'PA'
BEGIN
--verifica tamanho da IE
IF LEN(@Ds_Inscricao_Estadual) <> 9 OR SUBSTRING(@Ds_Inscricao_Estadual, 1, 2) <> '15'
RETURN(0);
--calcula primeiro digito verificador
SET @counter = 1;
SET @b = 9;
SET @soma = 0;
WHILE @counter < 9
BEGIN
SET @soma = @soma + ( SUBSTRING(@Ds_Inscricao_Estadual, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b - 1;
END
SET @dig = @soma % 11;
IF @dig <= 1
SET @dig = 0;
ELSE
SET @dig = 11 - @dig;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 9, 1)
RETURN (0);
END
--PA
-- verifica IE para o estado PA
ELSE IF @Ds_UF = 'PA'
BEGIN
--verifica tamanho da IE
IF LEN(@Ds_Inscricao_Estadual) <> 9 OR SUBSTRING(@Ds_Inscricao_Estadual, 1, 2) <> '15'
RETURN(0);
--calcula primeiro digito verificador
SET @counter = 1;
SET @b = 9;
SET @soma = 0;
WHILE @counter < 9
BEGIN
SET @soma = @soma + ( SUBSTRING(@Ds_Inscricao_Estadual, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b - 1;
END
SET @dig = @soma % 11;
IF @dig <= 1
SET @dig = 0;
ELSE
SET @dig = 11 - @dig;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 9, 1)
RETURN (0);
END
--PA
-- verifica IE para o estado PB
ELSE IF @Ds_UF = 'PB'
BEGIN
--verifica tamanho da IE
IF LEN(@Ds_Inscricao_Estadual) <> 9
RETURN(0);
--calcula primeiro digito verificador
SET @counter = 1;
SET @b = 9;
SET @soma = 0;
WHILE @counter < 9
BEGIN
SET @soma = @soma + ( SUBSTRING(@Ds_Inscricao_Estadual, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b - 1;
END
SET @dig = @soma % 11;
IF @dig <= 1
SET @dig = 0;
ELSE
SET @dig = 11 - @dig;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 9, 1)
RETURN (0);
END
--PB
-- verifica IE para o estado PR
ELSE IF @Ds_UF = 'PR'
BEGIN
--verifica tamanho da IE
IF LEN(@Ds_Inscricao_Estadual) <> 10
RETURN(0);
--calcula primeiro digito verificador
SET @counter = 1;
SET @b = 3;
SET @soma = 0;
WHILE @counter < 9
BEGIN
SET @soma = @soma + ( SUBSTRING(@Ds_Inscricao_Estadual, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b - 1;
IF @b = 1
SET @b = 7;
END
SET @dig = 11 - ( @soma % 11 );
IF @dig >= 10
SET @dig = 0;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 9, 1)
RETURN (0);
--calcula segundo digito verificador
SET @counter = 1;
SET @b = 4;
SET @soma = 0;
WHILE @counter < 10
BEGIN
SET @soma = @soma + ( SUBSTRING(@Ds_Inscricao_Estadual, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b - 1;
IF @b = 1
SET @b = 7;
END
SET @dig = 11 - ( @soma % 11 );
IF @dig >= 10
SET @dig = 0;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 10, 1)
RETURN (0);
END
--PR
-- verifica IE para o estado PE
ELSE IF @Ds_UF = 'PE'
BEGIN
--verifica tamanho da IE
IF LEN(@Ds_Inscricao_Estadual) <> 9
RETURN(0);
--calcula primeiro digito verificador
SET @counter = 1;
SET @b = 8;
SET @soma = 0;
WHILE @counter < 8
BEGIN
SET @soma = @soma + ( SUBSTRING(@Ds_Inscricao_Estadual, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b - 1;
END
SET @dig = 11 - ( @soma % 11 );
IF @dig >= 10
SET @dig = 0;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 8, 1)
RETURN (0);
--calcula segundo digito verificador
SET @counter = 1;
SET @b = 9;
SET @soma = 0;
WHILE @counter < 9
BEGIN
SET @soma = @soma + ( SUBSTRING(@Ds_Inscricao_Estadual, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b - 1;
END
SET @dig = 11 - ( @soma % 11 );
IF @dig >= 10
SET @dig = 0;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 9, 1)
RETURN (0);
END
--PE
-- verifica IE para o estado PI
ELSE IF @Ds_UF = 'PI'
BEGIN
--verifica tamanho da IE
IF LEN(@Ds_Inscricao_Estadual) <> 9
RETURN(0);
--calcula primeiro digito verificador
SET @counter = 1;
SET @b = 9;
SET @soma = 0;
WHILE @counter < 9
BEGIN
SET @soma = @soma + ( SUBSTRING(@Ds_Inscricao_Estadual, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b - 1;
END
SET @dig = @soma % 11;
IF @dig <= 1
SET @dig = 0;
ELSE
SET @dig = 11 - @dig;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 9, 1)
RETURN (0);
END
--PI
-- verifica IE para o estado RJ
ELSE IF @Ds_UF = 'RJ'
BEGIN
--verifica tamanho da IE
IF LEN(@Ds_Inscricao_Estadual) <> 8
RETURN(0);
--calcula primeiro digito verificador
SET @counter = 1;
SET @b = 2;
SET @soma = 0;
WHILE @counter < 8
BEGIN
SET @soma = @soma + ( SUBSTRING(@Ds_Inscricao_Estadual, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b - 1;
IF @b = 1
SET @b = 7;
END
SET @dig = @soma % 11;
IF @dig <= 1
SET @dig = 0;
ELSE
SET @dig = 11 - @dig;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 8, 1)
RETURN (0);
END
--RJ
-- verifica IE para o estado RN
ELSE IF @Ds_UF = 'RN'
BEGIN
--verifica tamanho da IE
IF LEN(@Ds_Inscricao_Estadual) <> 9
RETURN(0);
--calcula primeiro digito verificador
SET @counter = 1;
SET @b = 9;
SET @soma = 0;
WHILE @counter < 9
BEGIN
SET @soma = @soma + ( SUBSTRING(@Ds_Inscricao_Estadual, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b - 1;
END
SET @soma = @soma * 10;
SET @dig = @soma % 11;
IF @dig = 10
SET @dig = 0;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 9, 1)
RETURN (0);
END
--RN
-- verifica IE para o estado RS
ELSE IF @Ds_UF = 'RS'
BEGIN
--verifica tamanho da IE
IF LEN(@Ds_Inscricao_Estadual) <> 10 OR SUBSTRING(@Ds_Inscricao_Estadual, 1, 3) > '467'
RETURN(0);
--calcula primeiro digito verificador
SET @counter = 1;
SET @b = 2;
SET @soma = 0;
WHILE @counter < 10
BEGIN
SET @soma = @soma + ( SUBSTRING(@Ds_Inscricao_Estadual, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b - 1;
IF @b = 1
SET @b = 9;
END
SET @dig = 11 - ( @soma % 11 );
IF @dig >= 10
SET @dig = 0;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 10, 1)
RETURN (0);
END
--RS
-- verifica IE para o estado RO
ELSE IF @Ds_UF = 'RO'
BEGIN
--verifica tamanho da IE
IF LEN(@Ds_Inscricao_Estadual) <> 14
RETURN(0);
--calcula primeiro digito verificador
SET @counter = 1;
SET @b = 6;
SET @soma = 0;
WHILE @counter < 14
BEGIN
SET @soma = @soma + ( SUBSTRING(@Ds_Inscricao_Estadual, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b - 1;
IF @b = 1
SET @b = 9;
END
SET @dig = 11 - ( @soma % 11 );
IF @dig >= 10
SET @dig = @dig - 10;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 14, 1)
RETURN (0);
END
--RO
-- verifica IE para o estado RR
ELSE IF @Ds_UF = 'RR'
BEGIN
--verifica tamanho da IE
IF LEN(@Ds_Inscricao_Estadual) <> 9 OR SUBSTRING(@Ds_Inscricao_Estadual, 1, 2) <> '24'
RETURN(0);
--calcula primeiro digito verificador
SET @counter = 1;
SET @b = 1;
SET @soma = 0;
WHILE @counter < 9
BEGIN
SET @soma = @soma + ( SUBSTRING(@Ds_Inscricao_Estadual, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b + 1;
END
SET @dig = @soma % 9;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 9, 1)
RETURN (0);
END
--RR
-- verifica IE para o estado SC
ELSE IF @Ds_UF = 'SC'
BEGIN
--verifica tamanho da IE
IF LEN(@Ds_Inscricao_Estadual) <> 9
RETURN(0);
--calcula primeiro digito verificador
SET @counter = 1;
SET @b = 9;
SET @soma = 0;
WHILE @counter < 9
BEGIN
SET @soma = @soma + ( SUBSTRING(@Ds_Inscricao_Estadual, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b - 1;
END
SET @dig = @soma % 11;
IF @dig <= 1
SET @dig = 0;
ELSE
SET @dig = 11 - @dig;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 9, 1)
RETURN (0);
END
--SC
-- verifica IE para o estado SP
ELSE IF @Ds_UF = 'SP'
BEGIN
IF SUBSTRING(@Ds_Inscricao_Estadual, 1, 1) = 'P'
BEGIN
SET @die = SUBSTRING(@Ds_Inscricao_Estadual, 2, 9);
SET @soma = ( SUBSTRING(@die, 1, 1) * 1 ) + ( SUBSTRING(@die, 2, 1) * 3 ) + ( SUBSTRING(@die, 3, 1) * 4 ) + ( SUBSTRING(@die, 4, 1) * 5 ) + ( SUBSTRING(@die, 5, 1) * 6 ) + ( SUBSTRING(@die, 6, 1) * 7 ) + ( SUBSTRING(@die, 7, 1) * 8 ) + ( SUBSTRING(@die, 8, 1) * 10 );
SET @dig = @soma % 11;
IF @dig >= 10
SET @dig = 0;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 10, 1)
RETURN (0);
END
ELSE
BEGIN
IF LEN(@Ds_Inscricao_Estadual) < 12
RETURN(0);
SET @soma = ( SUBSTRING(@Ds_Inscricao_Estadual, 1, 1) * 1 ) + ( SUBSTRING(@Ds_Inscricao_Estadual, 2, 1) * 3 ) + ( SUBSTRING(@Ds_Inscricao_Estadual, 3, 1) * 4 ) + ( SUBSTRING(@Ds_Inscricao_Estadual, 4, 1) * 5 ) + ( SUBSTRING(@Ds_Inscricao_Estadual, 5, 1) * 6 ) + ( SUBSTRING(@Ds_Inscricao_Estadual, 6, 1) * 7 ) + ( SUBSTRING(@Ds_Inscricao_Estadual, 7, 1) * 8 ) + ( SUBSTRING(@Ds_Inscricao_Estadual, 8, 1) * 10 );
SET @dig = @soma % 11;
IF @dig >= 10
SET @dig = 0;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 9, 1)
RETURN (0);
SET @counter = 1;
SET @b = 3;
SET @soma = 0;
WHILE @counter < 12
BEGIN
SET @soma = @soma + ( SUBSTRING(@Ds_Inscricao_Estadual, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b - 1;
IF @b = 1
SET @b = 10;
END
SET @dig = @soma % 11;
IF @dig >= 10
SET @dig = 0;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 12, 1)
RETURN (0);
END
END
--SP
-- verifica IE para o estado SE
ELSE IF @Ds_UF = 'SE'
BEGIN
--verifica tamanho da IE
IF LEN(@Ds_Inscricao_Estadual) <> 9
RETURN(0);
--calcula primeiro digito verificador
SET @counter = 1;
SET @b = 9;
SET @soma = 0;
WHILE @counter < 9
BEGIN
SET @soma = @soma + ( SUBSTRING(@Ds_Inscricao_Estadual, @counter, 1) * @b );
SET @counter = @counter + 1;
SET @b = @b - 1;
END
SET @dig = 11 - ( @soma % 11 );
IF @dig >= 10
SET @dig = 0;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 9, 1)
RETURN (0);
END
--SE
-- verifica IE para o estado TO
ELSE IF @Ds_UF = 'TO'
BEGIN
--verifica tamanho da IE
IF LEN(@Ds_Inscricao_Estadual) <> 11 OR SUBSTRING(@Ds_Inscricao_Estadual, 3, 2) NOT IN ( '01', '02', '03', '99' )
RETURN(0);
--calcula primeiro digito verificador
SET @counter = 1;
SET @b = 9;
SET @soma = 0;
WHILE @counter < 11
BEGIN
IF @counter NOT IN ( 3, 4 )
BEGIN
SET @soma = @soma + ( SUBSTRING(@Ds_Inscricao_Estadual, @counter, 1) * @b );
SET @b = @b - 1;
END
SET @counter = @counter + 1;
END
SET @dig = @soma % 11;
IF @dig < 2
SET @dig = 0;
ELSE
SET @dig = 11 - @dig;
IF @dig <> SUBSTRING(@Ds_Inscricao_Estadual, 11, 1)
RETURN (0);
END
--TO
ELSE
RETURN(0);
END
ELSE
RETURN(0);
--retorna true
RETURN(1);
END
And that's it folks,
Until next time.
Dirceu Resende
Database & BI Architect · Microsoft MVP · MCSE, MCSA, MCT, MTA, MCP.
Comentários (0)
Carregando comentários…