关注图老师设计创意栏目可以让大家能更好的了解电脑,知道有关于电脑的更多有趣教程,今天给大家分享SQLServer中一个多用户自动生成编号的过程教程,希望对大家能有一点小小的帮助。
【 tulaoshi.com - 编程语言 】
  if not exists (select * from dbo.sysobjects where id = object_id(N'[IndexTable]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
  create table IndexTable(Ex char(20), num integer)
go
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)  create procedure SetIndex @Ex char(20),@result char(30) output,@Fmt integer
  as
    declare @num char(10)
    SET NOCOUNT on
    if not exists(select num from indextable where Ex=@ex )
     insert into indextable values(@ex,1)
    else
     update indextable set num=num+1
    select @num=cast(num as char(10)) from indextable where ex=@ex 
    select @num=space(@fmt-len(@num))+@num
    select @num=replace(@num,' ','0')
    select @result=rtrim(@ex)+rtrim(@num)
    SET NOCOUNT off
  go
  
  --------
  
  在delphi中调用
  
  procedure TForm1.Button1Click(Sender: TObject);
  begin
    StoredProc1.ParamByName('@Ex').AsString:='User';
    StoredProc1.ParamByName('@fmt').AsInteger:=3;
    StoredProc1.ExecProc;
    showmessage(StoredProc1.ParamByName('@result').value)
  end;
  
  -----------
  参数@ex表示前缀,@fmt表示数字长度,@result表示返回数据
  返回User001
来源:http://www.tulaoshi.com/n/20160219/1603765.html
看过《SQLServer中一个多用户自动生成编号的过程》的人还看了以下文章 更多>>