Vous êtes sur la page 1sur 6

-- Function: f_validarcedula(character varying)

-DROP FUNCTION f_validarcedula(character varying);


CREATE OR REPLACE FUNCTION f_validarcedula(p_cedula character varying)
RETURNS character varying AS
$BODY$
DECLARE
arregloEntero Integer Array[9];
band Integer Array[9];
v_arrCoef Integer Array[9];
tercerDigito INTEGER;
tamano INTEGER;
verificador Integer;
suma integer;
digito integer;
aux1 VARCHAR;
aux2 VARCHAR;
aux3 VARCHAR;
aux4 VARCHAR;
aux5 VARCHAR;
aux6 VARCHAR;
aux7 VARCHAR;
aux8 VARCHAR;
aux9 VARCHAR;
aux10 VARCHAR;
mod INTEGER;
begin
RAISE NOTICE
'Adding movie to the database' ;
RAISE NOTICE 'Guaichaaaa' ;
for i in 1..9 loop
if i=1 then

v_arrCoef[i]:=2;
end if;
if i=2 then
v_arrCoef[i]:=1;
end if;
if i=3 then
v_arrCoef[i]:=2;
end if;
if i=4 then
v_arrCoef[i]:=1;
end if;
if i=5 then
v_arrCoef[i]:=2;
end if;
if i=6 then
v_arrCoef[i]:=1;
end if;
if i=7 then
v_arrCoef[i]:=2;
end if;
if i=8 then
v_arrCoef[i]:=1;
end if;
if i=9 then
v_arrCoef[i]:=2;
end if;

end loop;
for i in 1..10 loop
arregloEntero[i]:=1;

if i=1 then
aux1:=substring(p_cedula from 1 for 1);
arregloEntero[i]:=aux1;
RAISE NOTICE 'fila = %',arregloEntero[i];
end if;
if i=2 then
aux2:=substring(p_cedula from 2 for 1);
arregloEntero[i]:=aux2;
RAISE NOTICE 'fila = %',arregloEntero[i];
end if;
if i=3 then
aux3:=substring(p_cedula from 3 for 1);
arregloEntero[i]:=aux3;
RAISE NOTICE 'fila = %',arregloEntero[i];
end if;
if i=4 then
aux4:=substring(p_cedula from 4 for 1);
arregloEntero[i]:=aux4;
RAISE NOTICE 'fila = %',arregloEntero[i];
end if;
if i=5 then
aux5:=substring(p_cedula from 5 for 1);
arregloEntero[i]:=aux5;
RAISE NOTICE 'fila = %',arregloEntero[i];
end if;
if i=6 then
aux6:=substring(p_cedula from 6 for 1);
arregloEntero[i]:=aux6;
RAISE NOTICE 'fila = %',arregloEntero[i];
end if;

if i=7 then
aux7:=substring(p_cedula from 7 for 1);
arregloEntero[i]:=aux7;
RAISE NOTICE 'fila = %',arregloEntero[i];
end if;
if i=8 then
aux8:=substring(p_cedula from 8 for 1);
arregloEntero[i]:=aux8;
RAISE NOTICE 'fila = %',arregloEntero[i];
end if;
if i=9 then
aux9:=substring(p_cedula from 9 for 1);
arregloEntero[i]:=aux9;
RAISE NOTICE 'fila = %',arregloEntero[i];
end if;
if i=10 then
aux10:=substring(p_cedula from 10 for 1);
arregloEntero[i]:=aux10;
RAISE NOTICE 'fila = %',arregloEntero[i];
end if;

end loop;
tamano:=10;
tercerDigito:=cast(substr(p_cedula,3,1) AS integer);
verificador:=cast(substr(p_cedula,10,1) AS integer);
suma:=0;
digito:=0;
band[1]:=tercerDigito;
band[2]:=tamano;
RAISE NOTICE 'terD= %',band[1];

RAISE NOTICE 'verD= %',band[2];


if(tamano=10) then
if(tercerDigito<6) then
for i in 1..(9) loop
digito:=(cast(substr(p_cedula,(i),1) as integer))*(v_arrCoef[i]);
suma:=suma+(mod(digito,10))+trunc(digito/10);
mod:=suma;
band[3]:=suma;
band[4]:=digito;
RAISE NOTICE 'digito= %',band[4];
RAISE NOTICE 'suma= %',band[3];
end loop;
if((mod(suma,10))=0) and ((mod(suma,10))=verificador)then
return 'El numero de la cedula '||p_cedula||' es correcto';
else
if ((10 - mod(suma,10))= verificador)then
return 'El numero de la cedula '||p_cedula||' es correcto';
else
return 'El numero de la cedula '||p_cedula||' es incorrecto';
end if;
end if;
else
return 'El numero de la cedula '||p_cedula||' es incorrecto';
end if;
else
return 'El numero ingresado debe tener 10 digitos, y tiene: '||tamano||' digitos
';
end if;
end
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;

ALTER FUNCTION f_validarcedula(character varying)


OWNER TO postgres;

Vous aimerez peut-être aussi