具体规范详见GB 11714-1997 全国组织机构代码编制规则.pdf
验证ORGCODE是否合法,合法返回1,不合法返回0
形如'66195133X' 或是 '66195133-X'
组织机构代码是每一个机关、社会团体、企事业单位在全国范围内唯一的、始终不变的法定代码标识。
最新使用的组织机构代码在1997年颁布实施,由8位数字(或大写拉丁字母)本体代码和1位数字(或大写拉丁字母)校验码组成。本体代码采用系列(即分区段)顺序编码方法。校验码按下列公式计算:
8
C9 = 11 - MOD ( ∑Ci * Wi ,11) … (2)
i=1
其中:MOD —— 表示求余函数;
i —— 表示代码字符从左到右位置序号;
Ci —— 表示第i位置上的代码字符的值,采用附录A“代码字符集”所列字符;
C9 —— 表示校验码;
Wi —— 表示第i位置上的加权因子,其数值如下表:
i 1 2 3 4 5 6 7 8
Wi 3 7 9 10 5 8 4 2
当MOD函数值为1(即 C9 = 10)时,校验码用字母X表示。
DROP FUNCTION DWPROC.FUN_ORGCODE_CHK;
CREATE FUNCTION DWPROC.FUN_ORGCODE_CHK
(I_ORGCODE VARCHAR(32)
)
RETURNS INTEGER
LANGUAGE SQL
NOT DETERMINISTIC
READS SQL DATA
STATIC DISPATCH
CALLED ON NULL INPUT
EXTERNAL ACTION
INHERIT SPECIAL REGISTERS
BEGIN ATOMIC
DECLARE v_orgcode VARCHAR(32);
DECLARE v_i INTEGER DEFAULT 1;
DECLARE v_total_01 INTEGER DEFAULT 0;
DECLARE v_total_02 INTEGER DEFAULT 0;
DECLARE v_total_03 INTEGER DEFAULT 0;
DECLARE v_total_04 INTEGER DEFAULT 0;
SET v_orgcode = LTRIM(RTRIM(I_ORGCODE));
IF LENGTH(v_orgcode) = 9 THEN
WHILE v_i <= 8 DO
SET v_total_01 = MOD(Power(2, ((10 - v_i) - 1)), 11);
IF ASCII(SUBSTR(v_orgcode,v_i,1)) BETWEEN 48 AND 57 THEN
SET v_total_02 = ASCII(SUBSTR(v_orgcode,v_i,1)) - 48;
ELSEIF ASCII(SUBSTR(v_orgcode,v_i,1)) BETWEEN 65 AND 90 THEN
SET v_total_02 = ASCII(SUBSTR(v_orgcode,v_i,1)) - 55;
ELSE RETURN 0;
END IF;
SET v_total_03 = v_total_03 + v_total_01 * v_total_02;
SET v_i = v_i + 1 ;
END WHILE;
SET v_total_04 = 11 - MOD(v_total_03,11);
IF v_total_04 = 10 THEN
IF SUBSTR(v_orgcode,9,1) = 'X' THEN
RETURN 1;
ELSE RETURN 0;
END IF;
ELSEIF v_total_04 BETWEEN 0 AND 9 THEN
IF ASCII(SUBSTR(v_orgcode,9,1)) BETWEEN 48 AND 57 THEN
IF v_total_04 = ASCII(SUBSTR(v_orgcode,9,1)) - 48 THEN
RETURN 1;
ELSE RETURN 0;
END IF;
ELSE RETURN 0;
END IF;
ELSE RETURN 0;
END IF;
ELSEIF LENGTH(v_orgcode) = 10 AND SUBSTR(v_orgcode,9,1) = '-' THEN
WHILE v_i <= 8 DO
SET v_total_01 = MOD(Power(2, ((10 - v_i) - 1)), 11);
IF ASCII(SUBSTR(v_orgcode,v_i,1)) BETWEEN 48 AND 57 THEN
--CONCAT
SET v_total_02 = ASCII(SUBSTR(v_orgcode,v_i,1)) - 48;
ELSEIF ASCII(SUBSTR(v_orgcode,v_i,1)) BETWEEN 65 AND 90 THEN
SET v_total_02 = ASCII(SUBSTR(v_orgcode,v_i,1)) - 55;
ELSE RETURN 0;
END IF;
SET v_total_03 = v_total_03 + v_total_01 * v_total_02;
SET v_i = v_i + 1 ;
END WHILE;
SET v_total_04 = 11 - MOD(v_total_03,11);
IF v_total_04 = 10 THEN
IF SUBSTR(v_orgcode,10,1) = 'X' THEN
RETURN 1;
ELSE RETURN 0;
END IF;
ELSEIF v_total_04 BETWEEN 0 AND 9 THEN
IF ASCII(SUBSTR(v_orgcode,10,1)) BETWEEN 48 AND 57 THEN
IF v_total_04 = ASCII(SUBSTR(v_orgcode,10,1)) - 48 THEN
RETURN 1;
ELSE RETURN 0;
END IF;
ELSE RETURN 0;
END IF;
ELSE RETURN 0;
END IF;
ELSE RETURN 0;
END IF;
END;
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12801008/viewspace-1023540/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/12801008/viewspace-1023540/