最好在存储过程中用如下的代码得到这个新的ID:
DECLARE @TMP_ID INT
SET @TMP_ID = IDENT_CURRENT('BID_EvaluateItem')
IF ((@TMP_ID IS NOT NULL) AND (@TMP_ID >0))
BEGIN
--其它的操作
END

解决方案 »

  1.   

    不好意思修改如下:DECLARE @TMP_ID INT
    SET @TMP_ID = IDENT_CURRENT('表名')
    IF ((@TMP_ID IS NOT NULL) AND (@TMP_ID >0))
    BEGIN
    --其它的操作
    END
      

  2.   

    @@IDENTITY
    返回最后插入的标识值。语法
    @@IDENTITY返回类型
    numeric注释
    在一条 INSERT、SELECT INTO 或大容量复制语句完成后,@@IDENTITY 中包含此语句产生的最后的标识值。若此语句没有影响任何有标识列的表,则 @@IDENTITY 返回 NULL。若插入了多个行,则会产生多个标识值,@@IDENTITY 返回最后产生的标识值。如果此语句激发一个或多个执行产生标识值的插入操作的触发器,则语句执行后立即调用 @@IDENTITY 将返回由触发器产生的最后的标识值。若 INSERT 或 SELECT INTO 语句失败或大容量复制失败,或事务被回滚,则 @@IDENTITY 值不会还原为以前的设置。在返回插入到表的 @@IDENTITY 列的最后一个值方面,@@IDENTITY、SCOPE_IDENTITY 和 IDENT_CURRENT 函数类似。 @@IDENTITY 和 SCOPE_IDENTITY 将返回在当前会话的所有表中生成的最后一个标识值。但是,SCOPE_IDENTITY 只在当前作用域内返回值,而 @@IDENTITY 不限于特定的作用域。IDENT_CURRENT 不受作用域和会话的限制,而受限于指定的表。IDENT_CURRENT 返回任何会话和任何作用域中为特定表生成的标识值。有关更多信息,请参见 IDENT_CURRENT。示例
    下面的示例向带有标识列的表中插入一行,并用 @@IDENTITY 显示在新行中使用的标识值。INSERT INTO jobs (job_desc,min_lvl,max_lvl)
    VALUES ('Accountant',12,125)
    SELECT @@IDENTITY AS 'Identity'
      

  3.   

    select IDENT_CURRENT('TableName')
      

  4.   

    这么写存储过程
    declare @id int
    insert into [Table] values(,,,)
    set @id=@@identity
    select @id as id
    在前台这么写
    SqlCommand cmd=new SqlCommand("存储过程",cnn);
    .
    .
    .
    object id=cmd.ExecScalor();
    这个id 就返回你刚插入得值了
      

  5.   

    select @@identity as cn insert into([Table]) values(........)
      

  6.   

    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.*/
      

  7.   

    总结:
    对于马上使用的刚才插入的新记录ID用SCOPE_IDENTITY()是最合适的;
    对于想要得到一系列的操作中最后得到的那个自增的ID最好用@@IDENTITY;
    对于想要得到一个表中的最后一个插入操作所产生的ID的最好用IDENT_CURRENT('TBName')