create table Dingdan
(
Did int identity(1,1) primary key,
D_cid int not null ,
Dcount int not null,
Dtime datetime not null,
foreign key(D_cid) references customer(Cid)
)insert into Dingdan (D_cid,Dcount,Dtime) values (34,340,getdate())表:
 Did    D_cid   Dcount   Dtime
  ...........
  5      34       340     2010-11-01 18:18:18.181
如何得到这个最新增加的一行的Did,也就是自增ID?

解决方案 »

  1.   


    select max(did) from table 
      

  2.   

    记录都是顺序添加的,所以新添加的记录应该在最后一条SELECT COUNT(*) FROM Dingdan
      

  3.   


    select * from table where did=(select max(did) from table)--如果都是按照getdate()插入的话还可以
    select * from table where Dtime=(select max(Dtime) from table)
      

  4.   

    Insert into 语句后,加以下语句:
    select SCOPE_IDENTITY() as NewID