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
|