一、校验码说明:
校验码的计算方法 |
||||||||||||||||||||||||||||||||||||||||||
1. 代码位置序号 2. 计算步骤
表1 校验码的计算方法 |
二、SQL实现:
-- =============================================
-- Author: <Maco Wang>
-- Create date: <2009-10-27 16:01>
-- Description: <计算条形码的校验码>
-- =============================================
create function [dbo].[Get_CheckCode]
(
@ActiveCode varchar(12)
)
returns varchar(13)
as
begin
declare @codelength int
set @codelength= len(@ActiveCode)
declare @curnum int;set @curnum=0
declare @temp1 int;set @temp1=0
declare @temp2 int;set @temp2=0
declare @locatnum int;set @locatnum=0
declare @code13 int
declare @i int;set @i=0
while(@i<@codelength)
begin
set @locatnum=@i+2;
set @curnum=floor(substring(@ActiveCode,@codelength-@i,1))
if(@locatnum%2=0)
set @temp1=@temp1+@curnum
else
set @temp2=@temp2+@curnum
set @i=@i+1
end
set @code13=10-(@temp1*3+@temp2)%10;
if(@code13=10)
set @code13=0
return @ActiveCode+cast(@code13 as varchar(1));
end
--测试示例
select dbo.[Get_CheckCode]('827123121212')
--运行结果
/*
8271231212124
*/