create table aa ([id] [int] IDENTITY (1, 1) NOT NULL)

解决方案 »

  1.   

    id [int] IDENTITY (1, 1)
      

  2.   

    贴:
    --自已做标识列的例子--流水号:日期+当日编号:--创建得到最大id的函数
    create function f_getid()
    returns varchar(13)
    as
    begin
    declare @id varchar(13),@dt varchar(8)
    select @dt=dt from v_getdate
    select @id=max(id) from tb where id like @dt+'-%'
    if @id is null
    set @id=@dt+'-'+'0001'
    else
    set @id=@dt+'-'+right('0000'+cast(cast(right(@id,4) as int)+1 as varchar),4)
    return(@id)
    end
    go--创建表
    create table tb(id varchar(20) default dbo.f_getid() primary key,name varchar(10))
    go--创建视图,得到当前日期(因为函数中不能使用getdate())
    create view v_getdate as select dt=convert(varchar,getdate(),112)
    go--插入记录测试
    insert into tb(name) values('张三')
    insert into tb(name) values('张四')
    insert into tb(name) values('张五')
    insert into tb(name) values('张六')
    insert into tb(name) values('张七')
    insert into tb(name) values('张八')
    insert into tb(name) values('张九')
    insert into tb(name) values('张十')--显示插入的结果
    select * from tb--删除部分记录
    delete from tb where name in('张五','张七','张八','张十')--显示删除后的结果
    select * from tb
    go--删除环境
    drop table tb
    drop view v_getdate
    drop function f_getid
      

  3.   

    例如:create table t1(a varchar(20) default(dbo.f_xqID()),b varchar(20))CREATE function dbo.f_xqID(@cardid varchar(20))
    returns varchar(20)
    as
    begin
    declare @maxID int,@newID varchar(20),@strLen int,@ssLen int,@listLen int
    set @ssLen=8
    if not exists(select 1 from userfillcard where cardaccount='Z1100001')
      set @newID='Z1100001' 
    else
    begin
          select @maxID=max(cast(right(cardaccount,len(cardaccount)-3) as int)) from userfillcard where iswhat=3 and cardid=@cardid and left(cardaccount,1)='Z'
          set @maxID=@maxID+1
          set @newID='Z11'+cast(@maxID as varchar(20))
          set @strLen=len(@newID)    
          set @listLen=@ssLen-@strLen
          while @listLen>0
          begin
                     set @newID=stuff(@newID,4,0,'0')
                     set @listLen=@listLen-1
          end
    end
    return (@newID)
    end
    这样生成的ID就是Z1100001,Z1100002,.......
      

  4.   

    insert into tb (id)
    select max(id)+1 from tb
      

  5.   

    insert 表 ( id ) select max(id) + 1 from 表
    --呵呵
      

  6.   

    我可能没说清楚,应该是表中已经存在记录,但是序号的列还没有值,我要在一次操作中用update将这个序号列都添加上序号,请问怎样实现?谢谢!
      

  7.   

    declare @i int
    set @i = 0
    update 表 set 序号列=@i,@i=@i+1
      

  8.   

    这个应该没问题的,你的序号列应为int型,若为字符型的话,改为
    declare @i int
    set @i = 0
    update 表 set 序号列=cast(@i as varchar(5)),@i=@i+1
      

  9.   

    没建表:   create tbale tablename (id int identity(1,1)表已建:   alter table tablename add id int identity(1,1)