问题是,如果定单号随机产生或则按年月日+序号的方式,你要定单号来干什么?还不如直接弄个“自动编码”的ID算了。如果有知道定单号,去查定单相关数据的情形发生,那ID也行啊?一般用SQL查定单信息的话,条件很少是定单号吧?也应该是‘where ueser = ... and datatime = ...’吧?
var CurDate,CurYear,CurMonth:string; Query:TADOQuery; begin Curdate:=leftstr(datetimetostr(now),pos(' ',datetimetostr(now))); CurYear:=leftstr(curdate,4); CurMonth:=leftstr(rightstr(curdate,length(curdate)-pos('-',curdate)),pos('-',rightstr(curdate,length(curdate)-pos('-',curdate)-1))); if length(Curmonth)=1 then Curmonth:='0'+Curmonth; Query:=TADOQuery.Create(self); //与数据库建立连接 Query.Connection:=dm.ADOConnection1; Query.SQL.Text:='select max(ProjectID) as ProjectID from Client_Info' + ' where left(Projectid,6)=''' + curyear+curmonth+''''; query.Open; if query.FieldByName('ProjectID').AsString ='' then // begin result:=curyear+curmonth+'0001'; end else if query.FieldByName('ProjectID').AsString <>'' then begin result:=inttostr(strtoint(query.FieldByName('ProjectID').AsString)+1); end; query.Close; query.Free; 用这个你试试看好了!
2.程序控制,如 WezZai(偶是湖南的) 所说
可以根据产品类型.编号.交易日期.时间.效验数等生成.本人认为订单号码随机生成不是很好,一点规律性也没有
CurDate,CurYear,CurMonth:string;
Query:TADOQuery;
begin
Curdate:=leftstr(datetimetostr(now),pos(' ',datetimetostr(now)));
CurYear:=leftstr(curdate,4);
CurMonth:=leftstr(rightstr(curdate,length(curdate)-pos('-',curdate)),pos('-',rightstr(curdate,length(curdate)-pos('-',curdate)-1)));
if length(Curmonth)=1 then Curmonth:='0'+Curmonth;
Query:=TADOQuery.Create(self);
//与数据库建立连接
Query.Connection:=dm.ADOConnection1; Query.SQL.Text:='select max(ProjectID) as ProjectID from Client_Info' +
' where left(Projectid,6)=''' + curyear+curmonth+'''';
query.Open;
if query.FieldByName('ProjectID').AsString ='' then //
begin
result:=curyear+curmonth+'0001';
end
else if query.FieldByName('ProjectID').AsString <>'' then
begin
result:=inttostr(strtoint(query.FieldByName('ProjectID').AsString)+1);
end;
query.Close;
query.Free;
用这个你试试看好了!
自己组合起来
例:流水号长度(5),起始流水号(1),前缀(A),后缀(B),流水号年(2005),流水号月(09),流水号日(02)
编码规则:前缀 流水号年(两位) 流水号月(两位) 则:A050900001,后5位是起始流水号,5位长是流水号长度,不够补0
编码规则:前缀 后缀 流水号年(两位) 流水号月(两位) 则:AB050900001
编码规则可以自己来设;