表A的字段ID,是自加类型的
用sql语句怎么知道当前ID的最大值?,因为当中有些ID的记录可能被删除
比如表A的记录:
ID
1
2
其中3,4的记录删除拉,所以sql查找当前ID的最大值应该是4,这个语句怎么写?

解决方案 »

  1.   

    加个字段如:[tem_Id],删除操作后为1 ,默认为0, 那你SELECT MAX(ID)时候就是4了.
      

  2.   


    select @max=max(id ) from 表 
      

  3.   

    肯定不是select @max=max(id ) from 表 这么简单select @max=max(id ) from 表会查是2而不是4嘛
      

  4.   

    你4这条纪录都已经删了,查MAX肯定是2了
      

  5.   

    用结果集显示不行,但可以在分析器中显示dbcc checkident(table)
      

  6.   

    dbcc checkident(table)
    这个方法查出来的是我要的结果,可是不知道怎么取,skyboy0720 能告诉我么
      

  7.   

    create table #T (ID int identity(1,1),F int)
    insert #T select 11
    insert #T select 22
    insert #T select 33
    insert #T select 44delete #T where id>2
    select * from #T
    /*
    ID          F           
    ----------- ----------- 
    1           11
    2           22
    */select maxid=ident_current('#T') --> 4drop table #T
      

  8.   

    IDENT_CURRENT 返回为任何会话和任何作用域中的特定表最后生成的标识值。
      

  9.   

    create table t(ID int identity(1,1),ID2 int)
    insert  t select 1
    insert  t select 1
    insert  t select 1
    insert  t select 1
    go
    delete t where ID>2
    go
    select * from t
    go
    检查标识信息: 当前标识值 '4',当前列值 '4'。
    DBCC 执行完毕。如果 DBCC 输出了错误信息,请与系统管理员联系。--drop table t
      

  10.   

    create table t(ID int identity(1,1),ID2 int)
    insert  t select 1
    insert  t select 1
    insert  t select 1
    insert  t select 1
    go
    delete t where ID>2
    go
    select * from tdrop table tDBCC CHECKIDENT ('t')---这个检查就行了