Function AutoCreateNum(titlestr:string;y,m:integer;num:integer):string; var s,str,maxnum:string; inum:integer; begin inum:=num; if (titlestr='')or (inttostr(num)='') then exit; result:=''; str:=inttostr(y)+'/'+inttostr(m); s:='select * from M_bomclzl_BK where parent_cpbh=:title order by test'; //數據表暫時未知 try with data_gc.qrypublic do begin close; sql.clear; sql.add(s); parambyname('title').asstring:=titlestr; prepare; open; end; except showmessage('查詢出錯!'); exit; end; if data_gc.qryPublic.IsEmpty=false then begin data_gc.qryPublic.Last; maxnum:=data_gc.qryPublic.fieldbyname('test').asstring; if str<>'0/0' then result:=data_gc.qryPublic.fieldbyname('parent_cpbh').asstring+'-'+str+'-'+GetLength(maxnum,inum) else result:=data_gc.qryPublic.fieldbyname('parent_cpbh').asstring+'-'+GetLength(maxnum,inum) end else if str<>'0/0' then result:=titlestr+'-'+str+'-'+GetLength('0',inum) else result:=titlestr+'-'+GetLength('0',inum);end; function GetLength(str:string;num:integer):string; var s:integer; Tstr:string; begin if num=0 then exit; s:=strtoint(str); s:=s+1; Tstr:=inttostr(s); while length(Tstr)<num do Tstr:='0'+Tstr; result:=Tstr; end;
得到8位数的订单号码.我在项目中用的function TDMFrm.GetMaxOrder(TableName, FirstNum: string; CodeField: string): string; var Order: string; Num: string; II: integer; begin ADSTemp.Close; ADSTemp.CommandText := 'Select Max(' + CodeField + ') from ' + TableName; ADSTemp.Open; Order := ADSTemp.Fields[0].AsString; if trim(order) = '' then Order := FirstNum + '000000' else begin Num := Copy(Order, 3, Length(Order) - 1); if Num = '000000' then II := 0 else II := StrToInt(Num); Inc(II); Order := FirstNum + Format('%.6d', [II]); end; Result := Order; end;
如果不用某种机制,可能会导致定单号重复。我觉得,如果用户少,那么,同时操作一个表的几率也就小,这样,简单的写一个获取定单表ID号的函数就可以了。
如果用户多,那么,我建议你做一个全局ID生成器。这样,不仅可以用于这个表,还可以用于你以后的任何一个系统。
var
s,str,maxnum:string;
inum:integer;
begin
inum:=num;
if (titlestr='')or (inttostr(num)='') then exit;
result:='';
str:=inttostr(y)+'/'+inttostr(m);
s:='select * from M_bomclzl_BK where parent_cpbh=:title order by test'; //數據表暫時未知
try
with data_gc.qrypublic do
begin
close;
sql.clear;
sql.add(s);
parambyname('title').asstring:=titlestr;
prepare;
open;
end;
except
showmessage('查詢出錯!');
exit;
end;
if data_gc.qryPublic.IsEmpty=false then
begin
data_gc.qryPublic.Last;
maxnum:=data_gc.qryPublic.fieldbyname('test').asstring;
if str<>'0/0' then
result:=data_gc.qryPublic.fieldbyname('parent_cpbh').asstring+'-'+str+'-'+GetLength(maxnum,inum)
else result:=data_gc.qryPublic.fieldbyname('parent_cpbh').asstring+'-'+GetLength(maxnum,inum)
end
else
if str<>'0/0' then
result:=titlestr+'-'+str+'-'+GetLength('0',inum)
else result:=titlestr+'-'+GetLength('0',inum);end;
function GetLength(str:string;num:integer):string;
var
s:integer;
Tstr:string;
begin
if num=0 then exit;
s:=strtoint(str);
s:=s+1;
Tstr:=inttostr(s);
while length(Tstr)<num do Tstr:='0'+Tstr; result:=Tstr;
end;
string;
var
Order: string;
Num: string;
II: integer;
begin
ADSTemp.Close;
ADSTemp.CommandText := 'Select Max(' + CodeField + ') from ' + TableName;
ADSTemp.Open;
Order := ADSTemp.Fields[0].AsString;
if trim(order) = '' then
Order := FirstNum + '000000'
else begin
Num := Copy(Order, 3, Length(Order) - 1);
if Num = '000000' then
II := 0
else
II := StrToInt(Num);
Inc(II);
Order := FirstNum + Format('%.6d', [II]);
end;
Result := Order;
end;
这个函数生成的单号由3部分组成:titlestr:单头; y,m:年,月; num:流水号长度.
调用函数时,指定单头.年月,流水号长度,则返回单号.如果指定的单头在数据表中不存在则从1开始编号。例如: AutoCreateNum('asdf','2003','11','5';
则返回:"asdf-2003/11-00001"。
能说清楚程序中用了哪些组件,用了什么数据库表,表的结构是怎么样的?
谢了!