@@IDENTITY
返回最后插入的标识值。IDENT_CURRENT
返回为任何会话和任何作用域中的指定表最后生成的标识值。
语法
IDENT_CURRENT('table_name')参数
table_name是将要返回其标识值的表的名称。table_name 的数据类型为 varchar,没有默认值。返回类型
sql_variantSCOPE_IDENTITY
返回插入到同一作用域中的 IDENTITY 列内的最后一个 IDENTITY 值。一个作用域就是一个模块——存储过程、触发器、函数或批处理。因此,如果两个语句处于同一个存储过程、函数或批处理中,则它们位于相同的作用域中。语法
SCOPE_IDENTITY( )返回类型
sql_variant

解决方案 »

  1.   

    SQL SERVER 有种自增长的数据类型,比oracle还方便,不用用户管,该字段可以自己自动设置值,插入的时候忽略该字段!!
      

  2.   

    CREATE TABLE BaseTable
      (PrimaryKey     int IDENTITY(1,1)
       Color          nvarchar(10) NOT NULL,
       Material       nvarchar(10) NOT NULL,
       ComputedCol AS (Color + Material)
      )
    GO--Create a view that contains all columns from the base table.
    CREATE VIEW InsteadView
    AS SELECT PrimaryKey, Color, Material, ComputedCol
    FROM BaseTable
    GO--Create an INSTEAD OF INSERT trigger on the view.
    CREATE TRIGGER InsteadTrigger on InsteadView
    INSTEAD OF INSERT
    AS
    BEGIN
      --Build an INSERT statement ignoring inserted.PrimaryKey and 
      --inserted.ComputedCol.
      INSERT INTO BaseTable
           SELECT Color, Material
           FROM inserted
    END
    GO直接引用 BaseTable 的 INSERT 语句不能为 PrimaryKey 和 ComputedCol 列提供值。例如:--A correct INSERT statement that skips the PrimaryKey and ComputedCol columns.
    INSERT INTO BaseTable (Color, Material)
           VALUES (N'Red', N'Cloth')--View the results of the INSERT statement.
    SELECT PrimaryKey, Color, Material, ComputedCol
    FROM BaseTable--An incorrect statement that tries to supply a value for the 
    --PrimaryKey and ComputedCol columns.
    INSERT INTO BaseTable
           VALUES (2, N'Green', N'Wood', N'GreenWood')然而,引用 InsteadView 的 INSERT 语句必须为 PrimaryKey 和 ComputedCol 列提供值:--A correct INSERT statement supplying dummy values for the 
    --PrimaryKey and ComputedCol columns.
    INSERT INTO InsteadView (PrimaryKey, Color, Material, ComputedCol)
           VALUES (999, N'Blue', N'Plastic', N'XXXXXX')
    --View the results of the INSERT statement.
    SELECT PrimaryKey, Color, Material, ComputedCol
    FROM InsteadView传递到 InsteadTrigger 的 inserted 表由不可为空的 PrimaryKey 和 ComputedCol 列构成,所以引用该视图的 INSERT 语句必须提供那些列的值。值 999 和 N'XXXXXX' 传递到 InsteadTrigger,但是触发器中的 INSERT 语句没有选择 inserted、PrimaryKey 或 ComputedCol,因此忽略该值。实际插入 BaseTable 的行在 PrimaryKey 中有 2,在 ComputedCol 有 N'BluePlastic'。在表上指定的 INSTEAD OF INSERT 触发器和在视图上指定的 INSTEAD OF 触发器,其 inserted 表中包含的计算列、标识列和 timestamp 列的值不同。