给你一个函数,是三年前做的。我赖得去精简了。
Function Getname(dateT:TDate):String; //获取编号
Var Max,Num,MaxNum,MinNum ,First,Second,II:Integer;
MaxsumNO,Date :String;
Function TDMF.EditToInt(Text:String):Integer;
Begin
try
Result :=StrToInt (Text);
except
Result :=0;
end;
End;
Function Int2StrPad0( N:longInt ;Len:Integer;Replace:String):String;
Begin
FmtStr(Result,'%d',[N]);
While Length(Result) <Len do
Result :=Replace+Result;
End;
Begin
Date :=FormatDatetime('yyyymmdd',DateT);
MaxsumNO :='';
Query:=TQuery.Create (Self);
try
MaxNum :=0;
Query.Close;
Query.SQL.Clear ;
Query.SQL.Add ('Select ''max''=max(ID) From 〈yourtable〉where ID like :NO ');
Query.ParamByName('NO').AsString :=Date+'%';
Query.Open;
MaxsumNO :=Query.FieldByName('max').AsString;
if not Query.EOF then Max :=EditToInt(copy(Query.FieldByName('max').AsString,9,4))
else Max :=0;
Query.Close;
Query.SQL.Clear ;
Query.SQL.Add ('Select ID from 〈yourtable〉 where ID like :NO order by ID');
Query.ParamByName('NO').AsString :=Date+'%';
Query.Open;
if not Query.EOF then Num:=Query.RecordCount
else Num :=0;
if Max>Num then
Begin
II:=EditToInt(copy(MaxsumNO,9,4))+1;
MaxsumNO :=Date+Int2StrPad0(II,4,'0');
Query.First ;
if not Query.EOF then
repeat
First :=EditToInt(Copy(Query.FieldByName('ID').AsString,9,4));
if First >MaxNum then MaxNum :=First;
if not Query.EOF then
Begin
Query.Next ;
Second :=EditToInt(Copy(Query.FieldByName('ID').AsString,9,4));
if Second>MaxNum then MaxNum :=Second ;
if First<>(Second-1) then
Begin
MaxNum :=First+1;
Result :=Date+Int2StrPad0(MaxNum,4,'0');
Break;
End;
Query.Prior ;
End;
if not Query.EOF then Query.Next ;
until Query.EOF ;
End
else
Result :=Date+Int2StrPad0(Num+1,4,'0');
End;