josephcnblog
--存储过程,自动生成一个工单号日期+3位流水号20170506001
IF OBJECT_ID(\'auto_mocodehand\')>0
   DROP PROC auto_mocodehand
GO
CREATE PROC auto_mocodehand
 --@Result float OUTPUT --要输出的值
AS   
declare @today varchar(100)
declare @count int
declare @resultcount int
declare @MoCodeHand varchar(100)
set @today=CONVERT(varchar(100), GETDATE(), 112)
--select @today

set @count=(select count(MoCodeHand) from planinfo where SUBSTRING( MoCodeHand , 1 , 8 )=CONVERT(varchar(100), GETDATE(), 112))
set @count=@count+1
set @MoCodeHand=@today+right(\'000000\'+CAST(@count as varchar(100)),3)
--select @MoCodeHand

set @resultcount=(select count(MoCodeHand) from planinfo where  MoCodeHand=@MoCodeHand)
while @resultcount>0
begin
	set @count=@count+1
	set @MoCodeHand=@today+right(\'000000\'+CAST(@count as varchar(100)),3)
	set @resultcount=(select count(MoCodeHand) from planinfo where  MoCodeHand=@MoCodeHand)
end
select @MoCodeHand
GO
--存储过程结束

EXECUTE auto_mocodehand

  

 

分类:

技术点:

相关文章: