日期:2014-05-17 浏览次数:20472 次
ALTER FUNCTION [dbo].[Fun_Ha_HaID] (@lctime datetime)
RETURNS char(19)
AS
BEGIN
DECLARE @lcMaxid char(4)
DECLARE @lcSysDate char(14)
DECLARE @lcLsh varchar(4)
SELECT @lcMaxid=IsNull(Max(Right(HaID,4)),'0'),
@lcSysDate=replace(replace(replace(CONVERT(varchar, @lctime, 120 ),'-',''),' ',''),':','')
FROM Ha_Data
WHERE HaID LIKE replace(replace(replace(CONVERT(varchar, @lctime, 120 ),'-',''),' ',''),':','')+'%'
IF Convert(Int,@lcMaxid)=0
SELECT @lcLsh='1'
ELSE
SELECT @lcLsh=Ltrim(Rtrim(Convert(char(4),Convert(Int,@lcMaxid)+1)))
RETURN @lcSysDate+'-'+Replicate('0',4-Len(@lcLsh))+@lcLsh
END
--查询下
select dbo.Fun_Ha_HaID(InDateTime) from Ha_Data
--没问题后执行
update Ha_Data set HaID=dbo.Fun_Ha_HaID(InDateTime)
DECLARE @ID int SELECT @ID=176
WHILE @ID<10647
BEGIN
UPDATE Ha_Data SET HaID=dbo.Fun_Ha_HaID(InDateTime) WHERE id=@ID
SET @id=@id+1
END