我看到一个插入数据的存储过程,在insert之后,单独写了一句:set @articleID = scope_identity(),查到了cope_identity,@@IDENTITY,IDENT_CURRENT的区别,我也明白这句话的意思了。
但是,之前我写存储过程,就没有这一句话,以为系统会自动生成一个identity的id(好像实际上也是如此)。
真实的情况是什么呢?如果不写这一句的话。

解决方案 »

  1.   

    SQL Server的自增列是系统自动生成的,这里的set @articleID = scope_identity()是为了获得之前产生并已经insert到表中的自增列的值,并使用其做进一步的操作。
      

  2.   


    写这句话,只是得到系统自动生成的 identity值 也就是说,你写不写它都会成生(如果你插记录的表里有标识列的话)
      

  3.   


    create table #table
    (
    t_id int identity(1,1),
    t_name nvarchar(50)
    )insert into #table (t_name) values('a')
    select scope_identity() as new_id--结果是刚生成的id:1
    insert into #table (t_name) values('b')
    select scope_identity() as new_id
    --结果是刚生成的id:2drop table #table