表tab字段有:
id   fid   name
1    1     aa
2    2     bb
其中id是个自动增加的字段,fid不是自增字段,但是值与id相同.现在从表tab中取name='aa'的记录回插入表tab中,结果如下:
id   fid   name
1    1     aa
2    2     bb
3    3     aainsert into tab(fid,name)
select ?,name from tab where name='aa';
fid的值怎么取?此时fid应该是id的最大值增1.

解决方案 »

  1.   

    --如果确保没有删除过记录。可以如下写:
    insert into tab(fid,name)
    select fid = (select max(fid) + 1 from tab) , name from tab where name='aa'
      

  2.   

    用这个更合适.--如果确保没有删除过记录。可以如下写:
    insert into tab(fid,name)
    select fid = (select max(id) + 1) , name from tab where name='aa';
      

  3.   

    --用这个更合适.上面那个错了.insert into tab(fid,name)
    select fid = (select max(id) + 1 from tab) , name from tab where name='aa'
      

  4.   

    insert tab(fid, name) select fid=ident_current('tab')+1, name from tab where name='aa'
      

  5.   

    用 ident_current('TableName') 函数:ident_current 返回为任何会话和任何作用域中的特定表最后生成的标识值。
      

  6.   

    楼兄的方法非常好。使用ident_current函数。
      

  7.   

    IDENT_CURRENT( 'table_name' )
     备注
    IDENT_CURRENT 类似于 SQL Server 2000 标识函数 SCOPE_IDENTITY 和 @@IDENTITY。这三个函数都返回最后生成的标识值。但是,上述每个函数中定义的“最后”的作用域和会话有所不同。 IDENT_CURRENT 返回为某个会话和用域中的指定表生成的最新标识值。
    @@IDENTITY 返回为跨所有作用域的当前会话中的某个表生成的最新标识值。
    SCOPE_IDENTITY 返回为当前会话和当前作用域中的某个表生成的最新标识值。
    在空表中调用 IDENT_CURRENT 函数时,此函数将返回 NULL。如果语句和事务失败,它们会更改表的当前标识,从而使标识列中的值出现不连贯现象。即使未提交试图向表中插入值的事务,也永远无法回滚标识值。例如,如果因 IGNORE_DUP_KEY 冲突而导致 INSERT 语句失败,表的当前标识值仍然会增加。 参数
    table_name其标识值被返回的表的名称。table_name 的数据类型为 varchar,无默认值。返回类型
    sql_variant 示例
    以下示例将显示由 IDENT_CURRENT、@@IDENTITY 和 SCOPE_IDENTITY 返回的不同标识值。 复制代码 
    USE AdventureWorks;
    GO
    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;
    SELECT @@IDENTITY;
    /*Returns the value 100. This was inserted by the trigger.*/SELECT SCOPE_IDENTITY();
    /* Returns the value 1. This was inserted by the 
    INSERT statement two statements before this query.*/SELECT IDENT_CURRENT('t7');
    /* Returns value inserted into t7, that is in the trigger.*/SELECT IDENT_CURRENT('t6');
    /* Returns value inserted into t6. This was the INSERT statement four statements before this query.*/-- Do the following in Session 2.
    SELECT @@IDENTITY;
    /* Returns NULL because there has been no INSERT action 
    up to this point in this session.*/SELECT SCOPE_IDENTITY();
    /* Returns NULL because there has been no INSERT action 
    up to this point in this scope in this session.*/SELECT IDENT_CURRENT('t7');
    /* Returns the last value inserted into t7.*/
     
      

  8.   

    2000 应该有 ident_current 这个函数吧,没有就用乌龟的方法,也是绝对正确的。
      

  9.   

    乌龟的方法也有错啊。
    分两步就可以解决了。insert into tab(fid,name)
    select 0,name from tab where name='aa'update tab
    set fid=id where fid=0