Search This Blog

Wednesday, August 29, 2012

User Define function for Unique ID


CREATE  FUNCTION [dbo].[Ordernumber]()
RETURNS varchar(20)
AS
BEGIN
DECLARE @_ggmvalue varchar(30)
DECLARE @_ggmvalue1 varchar(30)
DECLARE @_ggmvalue2 varchar(20)
DECLARE @_ggmmilisec varchar(5)
DECLARE @lengthmilisec varchar(5)
DECLARE @_TtmPrefix varchar(16)
DECLARE @_TtmSufix varchar(16)
DECLARE @_TtmSufix1 varchar(16)
DECLARE @_TtmId varchar(22)
DECLARE @length int
select @_ggmvalue1=convert(varchar(10),getdate(),112)+convert(varchar(10),getdate(),108)
select @_ggmmilisec=convert(varchar(10),getdate(),120)
set @length=Len(@_ggmmilisec)
if(@length<2)
begin
set @_ggmmilisec='000'+@_ggmmilisec
end
if(@length<3 and @length>=2)
begin
set @_ggmmilisec='00'+@_ggmmilisec
end
if(@length<4 and @length>=3)
begin
set @_ggmmilisec='0'+@_ggmmilisec
end
if(@length>=4)
begin
set @_ggmmilisec=@_ggmmilisec
end
select @_ggmvalue=@_ggmvalue1+@_ggmmilisec
select @_ggmvalue2= Replace(@_ggmvalue,':','')
select @_TtmSufix=RandNumber from vRandNumber
select @_TtmSufix1= Replace(@_TtmSufix,'.','')
select @_TtmPrefix=Substring(@_ggmvalue2,3,18)
set @_TtmId=@_TtmPrefix+convert(varchar(6),@_TtmSufix1)
return @_TtmId
END


** Create view for RandNumber
CREATE VIEW [dbo].[vRandNumber]
AS
SELECT RAND() as RandNumber
GO

No comments :