首页 > 数据库

生成带有校验位的唯一号

时间:2009-05-01 22:34:29  作者:dongshixiong  我要投稿
Linux初探欢迎您的投稿,投放方法请点击这里查看,我们会定期赠送精美小礼品给优秀的投稿作者。海纳百川 取则行远!LinuxGoo欢迎您的到来。
GO /****** 对象:UserDefinedFunction [dbo].[udf_CreateCheckID]脚本日期: 04/09/2009 14:43:54 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO /*......

GO
/****** 对象: UserDefinedFunction [dbo].[udf_CreateCheckID] 脚本日期: 04/09/2009 14:43:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*
产生校验位
唯一号由五部分构成
特征号(两位)

01,02,03,04
年:
2007(四位)
月:
02(两位)
序号:
1234567(七位)
校验位:
例如:
00,0000,00,0000000,(校验位)
从左开始数,奇数位乘1,偶数位乘3,然后相加除以10取余
为0则返回0
不为0则返回10减余数
*/
create FUNCTION [dbo].[udf_CreateUniqueid]
(@Category varchar(2),@SequenceId int)
returns varchar(16)
as
begin
declare @i int --循环计数
declare @j int
declare @strID varchar(15)
declare @CheckID int
set @i=1
set @CheckID=0
set @strID=@Category right('0000' cast(year(getdate()) as varchar(50)),4) right('00' cast(month(getdate()) as varchar(50)),2) right('0000000' cast(@SequenceId as varchar(7)),7)
begin
while @i<16
begin
set @j=convert(int,substring(@strID,@i,1))--SUBSTRING ( expression, start, length ) CONVERT ( data_type [ ( length ) ] , expression [ , style ] )
if @i%2=0
begin
set @CheckID=@CheckID @j*3
end
else
begin
set @CheckID=@CheckID @j*1
end
set @i=@i 1
end
if(@CheckID=0)
begin
return @strID '0'
end
else
begin
--return [email=10-@CheckID]10-@CheckID[/email]
return @strID cast([email=10-@CheckID]10-@CheckID[/email] as varchar)
end
end
return null
end



生成顺序号

declare @maxnum int
declare @index int
declare @temp nvarchar(50)
set @maxnum=1383000
set @index=0
while @index<@maxnum 1
begin
if(@index<99999)
begin
set @temp='Y' right('00000' cast(@index as varchar(50)),5)
end
else
begin
set @temp='Y' cast(@index as varchar(50))
end
insert table_1 (colume1)
values (@temp)
set @index=@index 1
end

如果您需转载 生成带有校验位的唯一号,请注明来自LinuxGoo.com,其版权归原作者所有。请广大网友留言时遵纪守法,使用文明用语。如果您在应用中有什么问题,请在下面留言,我们会尽快解答。
来顶一下
近回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
相关文章
栏目热门