在新增加记录时
要求自动生成6位整形ID 123456
12位表示年份,34位表示类型(自己定义),56位表示流水号(自动生成,累加)12年份根据输入时间自动生成,类型根据在添加记录时选择的类型自动生成,
流水号,如果前面4位代码相同自动增加1,没有相同的则从01开始记数。
如果生成的要求是字符形又该怎么做?如果要求的是16位字符形
前2位是类型(为英文字母),后面分别是年份,类型,流水号。
该怎么做?急啊。各位兄弟姐妹帮帮忙吧。最好给个例子!

解决方案 »

  1.   

    CREATE PROCEDURE SHa_mat_no_auto(@mata_no varchar(2),@mat_no_auto varchar(5) output)
    AS         --------------------生成配件編號
    declare @mat_no varchar(10),@no int,@mat1 varchar(3)
    declare @year varchar(2)
    set @year=datepart(yy,getdate())
    if (select count(*) from ha_mat where mata_no=@mata_no)<1 
    begin
        set @mat_no_auto=@year+@mata_no+'01'
        return
    end
    select  top 1 @mat_no=isnull(mat_no,@mata_no+'00') from ha_mat where mata_no=@mata_no order by mat_no desc
    set @mat1=substring(@mat_no,5,2)
    set @no=convert(int,@mat1)+1
    if @no>=100
    begin
        set @mat_no_auto='0'
        return
    end
    set @mat1=convert(varchar(2),@no)
    while len(@mat1)<2
      set @mat1='0'+@mat1
    set @mat_no_auto=@mata_no+@mat1GO
      

  2.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      i,j,m:integer;
      year,month,day:word;
      y,v,s,id:string;
    begin
      m:=0;
      decodedate(now,year,month,day);
      v:='fd'; ————————定义的变量类型,自己选
      y:=copy(inttostr(year),3,2);————代表年份的
      form1.Table1.Open;
      form1.Table1.First;
      i:=form1.Table1.RecordCount;
      if i>0 then
      begin
        for j:=0 to i-1 do
        begin
          s:=form1.Table1.FieldValues['id'];
          if copy(s,1,4)=y+v then
            m:=m+1;
        end;
        if m=0 then
         id:=y+v+'01'————最终的id
        else
        begin
          m:=m+1;
          if m<10 then
            id:=y+v+'0'+inttostr(m)
          else
            id:=y+v+inttostr(m);
        end;
      end
      else
        id:=y+v+'01';
    end;
      

  3.   

    在数据集的afterinsert中生成你要的流水号并赋值到记录对应字段就OK
      

  4.   

    強烈同意 lovelymelon(小人物)  的﹐在delphi中調用
      

  5.   

    楼上不妥,最好在服务器端进行,这样才可以避免不同的客户机产生同样的id,最好用存储过程和事务处理declare @a1 char(2)--前2位
    declare @a2 char(2)--中间2位
    select @a1 = datepart(yy,getdate())--得到前两位
    select @a2 = '34'--中间2位推出id的sql语句如下
    select (cast((@a1+@a2) as integer)*100 + IsNUll(max(id mod 100), 0 )  from table where id like @a1+@a2
      

  6.   

    谢谢大家了,特别是
     bbs791109(小别) ,lovelymelon(小人物) , firetoucher(风焱) 
    小弟是处学者
    对DELPHI的语法也不太熟。
    刚看了COPY的函数。
    麻烦哪位解释一下吧。特别是Index, Count,我不太明白。以下为原文Delphi syntax:function Copy(S; Index, Count: Integer): string;
    function Copy(S; Index, Count: Integer): array;DescriptionS is an expression of a string or dynamic-array type. Index and Count are integer-type expressions. Copy returns a substring or subarray containing Count characters or elements starting at S[Index]. The substring or subarray is a unique copy (that is, it does not share memory with S, although if the elements of the array are pointers or objects, these are not copied as well.)If Index is larger than the length of S, Copy returns an empty string or array.If Count specifies more characters or array elements than are available, only the characters or elements from S[Index] to the end of S are returned.Note: When S is a dynamic array, you can omit the Index and Count parameters and Copy copies the entire array.
      

  7.   

    function Copy(S; Index, Count: Integer): string;
    S:要拷贝的串,Index:要拷贝的开始位置,Count:要拷贝几个字符
    example:
    var
       s,s1:string;
       
    begin
       s:='123456';
       s1:=Copy(s,1,3);
       PrintLn(s1);
    end;
    结果是:123
      

  8.   

    楼上的兄弟说的不错.加上一句:如果你的写法copy(s,3,8)的话他就只现实从第三位后的那几位,即:3456其写法与copy(s,3,6)的结果是一样的
      

  9.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      i,j,m:integer;
      year,month,day:word;
      y,v,s,id:string;
    begin
      m:=0;
      decodedate(now,year,month,day);
      v:='fd'; ————————定义的变量类型,自己选
      y:=copy(inttostr(year),3,2);————代表年份的
      form1.Table1.Open;
      form1.Table1.First;
      i:=form1.Table1.RecordCount;
      if i>0 then
      begin
        for j:=0 to i-1 do
        begin
          s:=form1.Table1.FieldValues['id'];
          if copy(s,1,4)=y+v then
            m:=m+1;
        end;
        if m=0 then
         id:=y+v+'01'————最终的id
        else
        begin
          m:=m+1;
          if m<10 then
            id:=y+v+'0'+inttostr(m)
          else
            id:=y+v+inttostr(m);
        end;
      end
      else
        id:=y+v+'01';
    end;
      

  10.   

    同意firetoucher(风焱)的观点
    我也这么实现。利用存储过程,设置输入输出参数,用sql语句实现编号。这样确实可以避免因为多客户机同时增ID时的ID重复问题。
    原来使用的一个存储过程实例:CREATE PROCEDURE [dbo].[GetCode] 
    @begincode char(8),
    @ReturnCode char(12) OUTPUT
    AS
    if (select count(*) from ceshiMaster where bill_code like @begincode+'%')>0 
      begin
         select @ReturnCode=Max(bill_code) from ceshimaster 
              where bill_code like @begincode+'%'
         set @ReturnCode=right(@ReturnCode,4)+1 
         set @ReturnCode=@begincode+@ReturnCode
      end
    else
     SET @ReturnCode=@begincode+'1000'GO
      

  11.   

    我知道用存储过程比较好,但本人为初学
    认为lovelymelon(小人物)的办法比较易懂。
    但是他是用table组件做的,有没有办法用adoquery
    里的sql语句来做。
    因为我的这个程序是在查询的结果里添加
    只能用query