通常我们在做单据号码:
单据号+年月日+流水码
PO 081112 001
日期部分通常是用的是getdate()函数来处理。如果用户义函数中使用getdate函数将会收到这样的错误信息“在函数内部正确使用了'getdate'”,出现这样的错误的原因。用户定义函数不允许使用不确定函数,而getdate恰好是不确定函数
写一视图
Create View v_GetDate AS Select dt=Convert(Char(6),GetDate(),12)
一:存储过程
CREATE procedure UF_Web_TaskCode( @Type varchar(2) ) as begin declare @dt CHAR(8),@lsh char(3),@dt2 char(8),@temp char(8), @TaskCode varchar(11) Select @dt=@Type+(Select * from v_GetDate) select @lsh=substring(MAX(TaskCode),9,11)+1 from Task where substring(TaskCode,1,8)=@dt set @TaskCode=@dt+@temp if @lsh is null set @temp='001' else if @lsh<=9 set @temp='00'+convert(varchar,@lsh) else if @lsh<=99 set @temp='0'