刚刚插入数据库的帖子,如何立即得到它的自增ID  ACCESS的办法,和SQL2000的办法,都告诉我。多谢了

解决方案 »

  1.   

    fluxayxxx(阿茂) 效率太低,不予考虑
      

  2.   

    Sql:
    select @@IdentityAccess:
    Access中好像没有类似的函数,似乎只能使用 fluxayxxx(阿茂) 的方法
      

  3.   

    这样写INSERT语句insert into youtable (code,name) values ('aa','bb') select @@Identity 这样将返回刚插入的记录的自增ID
      

  4.   

    我在 SQL2000中是这样写的,不知道Access可不可以
    select max(id) from table
      

  5.   

    VB.net version:
    http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B815629c# version:
    http://support.microsoft.com/kb/816112/EN-US/
      

  6.   

    http://www.tn99.com/myblog/blogview.asp?logID=96&cateID=5
      

  7.   

    晕,在sql中可以直接获得进行操作的记录的主键@identity
    就是要找的id
      

  8.   

    ACCESS 里哪有 SELECT @@IDENTITY 这种写法 ?????
      

  9.   

    我在 SQL2000中是这样写的,不知道Access可不可以
    select max(id) from table
    如果有许多人同时操作呢??
      

  10.   

    buyifly(冬雷物语) 许多人同时做会有什么效果?
    效率低了? 小弟不明白,初学望指教。
      

  11.   

    在Access中,采用select max(id) from table
    只是在Select时,一定要先锁住数据库,读完后,再解锁
      

  12.   

    WilliamFire(寒枫天伤) 在Access中,采用select max(id) from table
    只是在Select时,一定要先锁住数据库,读完后,再解锁----------------------------------------------------------
    请问怎么先锁住数据库??
      

  13.   

    我是将SessionID同时写入这张表中,也就是加一列,如果选择记录时:
    Select Top 1 [ID] From Tables Where SessionID=MySessionID Order By 1 Desc
      

  14.   

    SELECT IDENT_CURRENT('yourtable') AS ID
      

  15.   

    sql 的话可以用存储过程直接返回加的ID,Access大概只能再写句返回最大ID了
      

  16.   

    如何得到SqlServer的自增ID:SqlServer中的自增的ID的最后的值:SELECT SCOPE_IDENTITY() --返回插入到同一作用域中的 IDENTITY 列内的最后一个 IDENTITY 值。
    SELECT @@IDENTITY   --返回插入到当前会话中任何作用域内的最后一个 IDENTITY 列值
    SELECT IDENT_CURRENT('TbName')--不受作用域和会话的限制,而受限于指定的表。IDENT_CURRENT 返回为任何会话和作用域中的特定表所生成的值。一个作用域就是一个模块——存储过程、触发器、函数或批处理。因此,如果两个语句处于同一个存储过程、函数或批处理中,则它们位于相同的作用域中。
    参考的例子如下:USE pubs
    DROP TABLE t6
    DROP TABLE t7GO
    CREATE TABLE t6(id int IDENTITY)
    CREATE TABLE t7(id int IDENTITY(100,1))
    GO
    CREATE TRIGGER t6ins ON t6 FOR INSERT 
    AS
    BEGIN
       INSERT t7 DEFAULT VALUES
       SELECT @@IDENTITY as [@@IDENTITY]
       SELECT SCOPE_IDENTITY() as [SCOPE_IDENTITY]
    END
    GO
    --end of trigger definitionSELECT   * FROM t6
    --id is empty.SELECT   * FROM t7
    --id is empty.
    --Do the following in Session 1
    INSERT t6 DEFAULT VALUES
    SELECT @@IDENTITY      
    /*Returns the value 100, which was inserted by the trigger.*/SELECT SCOPE_IDENTITY()   
    /* Returns the value 1, which was inserted by the 
    INSERT stmt 2 statements before this query.*/return
    SELECT IDENT_CURRENT('t7')
    /* Returns value inserted into t7, i.e. in the trigger.*/SELECT IDENT_CURRENT('t6')
    /* Returns value inserted into t6, which was the INSERT statement 4 stmts before this query.*/-- Do the following in Session 2
    SELECT @@IDENTITY
    /* Returns NULL since there has been no INSERT action 
    so far in this session.*/SELECT SCOPE_IDENTITY()
    /* Returns NULL since there has been no INSERT action 
    so far in this scope in this session.*/SELECT IDENT_CURRENT('t7')
    /* Returns the last value inserted into t7.*/
    总结:
    对于马上使用的刚才插入的新记录ID用SCOPE_IDENTITY()是最合适的;
    对于想要得到一系列的操作中最后得到的那个自增的ID最好用@@IDENTITY;
    对于想要得到一个表中的最后一个插入操作所产生的ID的最好用IDENT_CURRENT('TBName')
      

  17.   

    insert into table () values ()select @@identity
      

  18.   

    上面正解,但最好用一个输出参数来输出这个ID
    create proc aaaaa
    (
       @getid int output
    )
    as
    insert into table () values ()
    set @getid=@@identity
      

  19.   

    应该先确定id再插入
    用一个表保存id
      

  20.   

    感觉WilliamFire(寒枫天伤) 的方法效率不错,但是怎么锁access表,我也不知道。
      

  21.   

    hbxtlhx(下着春雨的天)说的没错