programing

SQL 데이터베이스에서 칠레산 RUT 확인

lastcode 2023. 8. 30. 21:46
반응형

SQL 데이터베이스에서 칠레산 RUT 확인

는 이 웹사이트에서 이 코드를 발견했고 SQL SERVER에서 잘 작동하고 있습니다.

    CREATE FUNCTION [dbo].[CalculaDigitoRut]
(
    @rut int
)
RETURNS char(1)
AS
BEGIN
   DECLARE @digito int
   DECLARE @contador int
   DECLARE @multiplo int
   DECLARE @acumulador int
   DECLARE @ret char

   set @contador = 2
   set @acumulador = 0
   while (@rut <> 0)
   begin
      set @multiplo = (@Rut % 10) * @contador
      set @acumulador = @acumulador + @multiplo 
      set @rut = @rut / 10 
      set @contador = @contador + 1 
      if (@contador = 8) 
         set @contador = 2 
   end
   set @digito = 11 - (@acumulador % 11) 

   if (@digito = 10)
      return ('K')
   if (@digito = 11)
      return ('0')
   return (@digito)
END

예상 결과

RUT --      validation_digit
10214564            K 
3781561             6
3433444             7
3066256             3

MariaDB(10.3.30-MariaDB) 구문에 맞게 수정하려고 했는데 몇 번 시도해도 구문 오류가 발생하지 않았지만 함수가 예상 결과를 반환하지 않습니다.

여기 마리아를 위한 새로운 코드가 있습니다.DB

CREATE FUNCTION Calculate_national_id_verifier (rut INT)
    RETURNS CHAR(1)
BEGIN
   DECLARE digito INT;
   DECLARE contador INT;
   DECLARE multiplo INT;
   DECLARE acumulador INT;
   DECLARE ret CHAR;

   SET contador = 2;
   SET acumulador = 0;
  
   WHILE rut <> 0 DO
      SET multiplo = (rut % 10) * contador; 
      SET acumulador = acumulador + multiplo;
      SET rut = rut / 10;
      SET contador = contador + 1; -- 3
      IF (contador = 8) THEN
          SET contador = 2;
      END IF;
   END WHILE;
   
   SET digito = 11 - (acumulador % 11);
   

   IF (digito = 10) THEN
      RETURN ('K');
   ELSEIF (digito = 11) THEN
      RETURN ('0');
   ELSE
      RETURN (digito);
   END IF;
END;

이 문제는 매우 멍청한 것 같습니다. 몇 시간 동안 비교해 봤는데 어디가 문제이고 왜 두 기능 사이에 차이가 있는지 찾을 수 없습니다.

저는 DBeaver 21.3.5를 클라이언트로 사용하여 쿼리를 실행하고 있습니다.

다음은 실제 결과입니다.

RUT --      validation_digit
10214564            6 
3781561             K
3433444             7
3066256             5

제가 실수를 했다면 이렇게 미안한 말을 하는 것은 처음입니다.저는 답을 찾을 수 없었습니다.

언급URL : https://stackoverflow.com/questions/71343791/validate-chilean-rut-in-a-sql-database

반응형