Created
May 3, 2017 14:55
-
-
Save hevertonfreitas/4c3212223c13e8dc8eaac7d75b6e9598 to your computer and use it in GitHub Desktop.
Gerador de CPF para plpgsql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION gerar_cpf() | |
RETURNS VARCHAR AS | |
$BODY$ | |
-- ROTINA DE GERAÇÃO DE CPF SEM LOOP | |
-- Retorna string com CPF aletório correto. | |
DECLARE | |
vet_cpf INTEGER [11]; --Recebe o CPF | |
soma INTEGER; -- Soma utilizada para o cálculo do DV | |
rest INTEGER; -- Resto da divisão | |
BEGIN | |
-- Atribuição dos valores do Vetor | |
vet_cpf [0] := cast(substring(CAST(random() AS VARCHAR), 3, 1) AS INTEGER); | |
vet_cpf [1] := cast(substring(CAST(random() AS VARCHAR), 3, 1) AS INTEGER); | |
vet_cpf [2] := cast(substring(CAST(random() AS VARCHAR), 3, 1) AS INTEGER); | |
vet_cpf [3] := cast(substring(CAST(random() AS VARCHAR), 3, 1) AS INTEGER); | |
vet_cpf [4] := cast(substring(CAST(random() AS VARCHAR), 3, 1) AS INTEGER); | |
vet_cpf [5] := cast(substring(CAST(random() AS VARCHAR), 3, 1) AS INTEGER); | |
vet_cpf [6] := cast(substring(CAST(random() AS VARCHAR), 3, 1) AS INTEGER); | |
vet_cpf [7] := cast(substring(CAST(random() AS VARCHAR), 3, 1) AS INTEGER); | |
vet_cpf [8] := cast(substring(CAST(random() AS VARCHAR), 3, 1) AS INTEGER); | |
-- CÁLCULO DO PRIMEIRO NÚMERO DO DV | |
-- Soma dos nove primeiros multiplicados por 10, 9, 8 e assim por diante... | |
soma := (vet_cpf [0] * 10) + | |
(vet_cpf [1] * 9) + | |
(vet_cpf [2] * 8) + | |
(vet_cpf [3] * 7) + | |
(vet_cpf [4] * 6) + | |
(vet_cpf [5] * 5) + | |
(vet_cpf [6] * 4) + | |
(vet_cpf [7] * 3) + | |
(vet_cpf [8] * 2); | |
rest := soma % 11; | |
IF (rest = 0) OR (rest = 1) | |
THEN | |
vet_cpf [9] := 0; | |
ELSE | |
vet_cpf [9] := (11 - rest); | |
END IF; | |
-- CÁLCULO DO SEGUNDO NÚMERO DO DV | |
-- Soma dos nove primeiros multiplicados por 11, 10, 9 e assim por diante... | |
soma := (vet_cpf [0] * 11) + | |
(vet_cpf [1] * 10) + | |
(vet_cpf [2] * 9) + | |
(vet_cpf [3] * 8) + | |
(vet_cpf [4] * 7) + | |
(vet_cpf [5] * 6) + | |
(vet_cpf [6] * 5) + | |
(vet_cpf [7] * 4) + | |
(vet_cpf [8] * 3) + | |
(vet_cpf [9] * 2); | |
rest := soma % 11; | |
IF (rest = 0) OR (rest = 1) | |
THEN | |
vet_cpf [10] := 0; | |
ELSE | |
vet_cpf [10] := (11 - rest); | |
END IF; | |
--Retorno do CPF | |
RETURN trim( | |
trim(to_char(vet_cpf [0], '9')) || | |
trim(to_char(vet_cpf [1], '9')) || | |
trim(to_char(vet_cpf [2], '9')) || | |
trim(to_char(vet_cpf [3], '9')) || | |
trim(to_char(vet_cpf [4], '9')) || | |
trim(to_char(vet_cpf [5], '9')) || | |
trim(to_char(vet_cpf [6], '9')) || | |
trim(to_char(vet_cpf [7], '9')) || | |
trim(to_char(vet_cpf [8], '9')) || | |
trim(to_char(vet_cpf [9], '9')) || | |
trim(to_char(vet_cpf [10], '9')) | |
); | |
END; | |
$BODY$ | |
LANGUAGE plpgsql; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment