有一个表:create table person
(
    id  int indentity(1, 1) not null unique,
    name varchar(20)
)
go这个表可能会在某一时刻被insert多条数据, 问题是如何才能够获取我插入的记录的id列的值,而不是其它人的??
如我插入一行数据: insert into person values('张三') 
可能同时会有许多人插入了数据如insert into person values('李四'),我如何才能得到张三的id而不是李四的id?
不知道我表述的清楚没有~

解决方案 »

  1.   

    @@IDENTITY
    返回最后插入的标识值的系统函数。
      

  2.   

    select id from tb where name='张三'这样不就知道了?
      

  3.   

    select @@identity  返回最后插入的标识值。...select id from 表 where name='张三'
      

  4.   

    我碰到过这种情况,这个id这时好像取不到。不过我没有深入我们的方法是每要插入一条数据就执行一个方法得到ID,然后把这ID插入这条数据,这样就可以取得到了
      

  5.   

    谢谢小梁,但是这样不会出现张冠李戴的情况吗?假如我插入了一条记录,然后去select @@IDENTITY,然而在执行我的select 时别人插入了一条记录,我会不会把别人的ID给读了?
      

  6.   


    select scope_identity()
      

  7.   

    @@IDENTITY和SCOPE_IDENTITY和IDENT_CURRENT的区别
    1,@@IDENTITY是得到当前会话的所有范围的最后插入的IDENTITY值 
    2,SCOPE_IDENTITY是得到当前会话的当前范围的最后插入的IDENTITY值 
    3,IDENT_CURRENT是得到指定表的最后插入的IDENTITY值,与会话、范围无关。因为在插入和得到IDENTITY值之间可能会有其它的事情发生,但是你只想得到我刚才插入的IDENTITTY值,只有使用SCOPE_IDENTITY函数才行。以下是测试SQL 
    (1)
    USE pubs
    DROP TABLE t6
    DROP TABLE t7
    GO
    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
    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
    declare @i int
    set @i = 0
     while @i < 20000
       begin
       set @i = @i + 1
       print @i
       endSELECT @@IDENTITY as "@@IDENTITY0"
    /*Returns the value 100, which was inserted by the trigger.*/SELECT SCOPE_IDENTITY()   'SCOPE_IDENTITY()0'
    /* Returns the value 1, which was inserted by the 
    INSERT stmt 2 statements before this query.*/SELECT IDENT_CURRENT('t7') 'IDENT_CURRENT(t7)0'
    /* Returns value inserted into t7, i.e. in the trigger.*/SELECT IDENT_CURRENT('t6') 'IDENT_CURRENT(t6)0'
    /* Returns value inserted into t6, which was the INSERT statement 4 stmts before this query.*/-- Do the following in Session 2
    SELECT @@IDENTITY "@@IDENTITY1"
    /* Returns NULL since there has been no INSERT action 
    so far in this session.*/SELECT SCOPE_IDENTITY() 'SCOPE_IDENTITY()1'
    /* Returns NULL since there has been no INSERT action 
    so far in this scope in this session.*/SELECT IDENT_CURRENT('t7') 'IDENT_CURRENT(t7)1'
    /* Returns the last value inserted into t7.*/
    当在执行while @i < 20000
       begin
       set @i = @i + 1
       print @i
       end这个地方的时候,就可以用
    INSERT t6 DEFAULT VALUES
    INSERT t7 DEFAULT VALUES
    select * from t6
    select * from t7来进行操作数据库等到全部执行完了,你就会发现只有SCOPE_IDENTITY列永远是1
      

  8.   

    网卡死...
    select id from tb where name='张三' 
    name不是主键啊!
      

  9.   

    这种时候我一般使用存储过程,因为不管同时刻插入多少,执行的时候还是按队列来的,存储过程中包含一条插入语句,然后再有一条选择这个ID的语句,加个order by id desc,就可以取得你插入的ID值,然后使用OUT参数返回即可.
      

  10.   

    在你插入数据后,紧接着select @@identity.必须是紧接着。
      

  11.   

    我用的是MSSQLServer 2000,并且不方便用存储过程,函数,触发器,该如何搞?
      

  12.   

    @@IDENTITY和scope_identity()都返回当前session中任一表产生的identity值。
    但是scope_identity()只返回当前范围中的identity值。
    因此楼主的情境中,应当使用scope_identity().
      

  13.   


    汗,上提到的session是什么意思,怎么个范围?
      

  14.   

    从老贴摘录的一段:
    在 INSERT 之前使用 SET NOCOUNT ON 语句,并将 SELECT @@IDENTITY 语句放在表中的 FOR INSERT 触发器中,如下面的代码片段所示。这样,任何进入该表的 INSERT 语句都将自动返回 IDENTITY 值。 CREATE TRIGGER trProducts_Insert ON Products FOR INSERT AS 
        SELECT @@IDENTITY 
    GO 
    触发器只在 Products 表上发生 INSERT 时启动,所以它总是会在成功 INSERT 之后返回一个 IDENTITY。使用此技术,您可以始终以相同的方式在应用程序中检索 IDENTITY 值。 我在查询分析器试了,果然insert后返回了IDENTITY,不过在具体的语言中却是如何将其获得,如在Java,C#中,insert不会返回表集呀?!