我的字段如下:
varchar id
datetime dtid        dt
0001  2001-01-02
0002  2001-01-02
0003  2001-01-02要求获取最大的id值,一定要完整显示如0003

解决方案 »

  1.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([id] varchar(4),[dt] datetime)
    insert [tb]
    select '0001','2001-01-02' union all
    select '0002','2001-01-02' union all
    select '0003','2001-01-02'select max(id) from [tb]
    --测试结果:
    /*
         
    ---- 
    0003(所影响的行数为 1 行)
    */
      

  2.   

    如果要完整显示整条记录
    select *
    from tb t
    where not exists(select 1 from tb where id>t.id)/*
    id   dt                                                     
    ---- ------------------------------------------------------ 
    0003 2001-01-02 00:00:00.000(所影响的行数为 1 行)*/