如表:     ID                  Person          Re
=====================================================
自动增长(int)            VARCHAR          TEXT
public int Insert(PersonMDL personMDL)
{
        DAL.Insert(personMDL);
        //返回ID的值?
} 请问这样可以吗??

解决方案 »

  1.   

    what is in  DAL.Insert? change it to retrieve the value, see检索“标识”或“自动编号”值
    http://msdn.microsoft.com/library/default.asp?url=/library/chs/cpguide/html/cpconRetrievingIdentityOrAutonumberValues.asp
      

  2.   

    有两种方法:第一种方法:
    insert into table1 values(colvalue1,colvalue2)select ident_current('table1')第二种方法:
    insert into table1 values(colvalue1,colvalue2)
    select @@identity
      

  3.   

    set recs=cnn.execute(“INSERT INTO Table1 SELECT(‘R1C1’,’R1C2’)”)
                         recordnum=cnn.execute(“SELECT @@IDENTITY”).fields(0).value
      

  4.   

    select @@identity
    就行 
    你封装到一个事务里面就行
      

  5.   

    这是我的存储过程:
    ============================================
    CREATE PROCEDURE dbo.WWFAction_Insert
    @ActionCode char(50),
    @ActionName varchar(50),
    @SolutionCode char(20),
    @SettingType int,
    @IsDelete int
    AS
    begin tran
    insert into WWFAction (ActionCode,ActionName,SolutionCode,SettingType,IsDelete) 
    values(@ActionCode,@ActionName,@SolutionCode,@SettingType,@IsDelete)
    if @@error!=0
    begin
    rollback
    end
    else
    begin
    commit
    end
    =========================================
    请问ID在哪里返回?是先执行完这个存储过程以后,再执行select @@identity SQL,还是执行完存储过程以后直接就返回ID了 ??
      

  6.   

    insert into table1 values(colvalue1,colvalue2)
    select SCOPE_IDENTITY()
    如果table1上没有触发器啥的也可用@@identity
      

  7.   

    CREATE PROCEDURE dbo.WWFAction_Insert
    @ActionCode char(50),
    @ActionName varchar(50),
    @SolutionCode char(20),
    @SettingType int,
    @IsDelete int
    AS
    begin tran
    insert into WWFAction (ActionCode,ActionName,SolutionCode,SettingType,IsDelete) 
    values(@ActionCode,@ActionName,@SolutionCode,@SettingType,@IsDelete) select @@IDENTITY
    if @@error!=0
    begin
    rollback
    end
    else
    begin
    commit
    end
    ==========================================================
    CREATE PROCEDURE dbo.WWFAction_Insert
    @ActionCode char(50),
    @ActionName varchar(50),
    @SolutionCode char(20),
    @SettingType int,
    @IsDelete int,
    @id int output
    AS
    begin tran
    insert into WWFAction (ActionCode,ActionName,SolutionCode,SettingType,IsDelete) 
    values(@ActionCode,@ActionName,@SolutionCode,@SettingType,@IsDelete)
    if @@error!=0
    begin
    rollback
    end
    else
    begin
                      select @id = @@IDENTITY
    commit
    end
    ===============================
    这两种写法有什么区别 ?
      

  8.   

    如果用ACCUSE,都没有存储过程,那怎么搞呢!
      

  9.   

    回 思归:
    DAL.Insert里是这样写的:
    public int Insert(PersonMDL personMDL)
    {
          SqlParameter [] parms = {
                                      Database.MakeInParam("@Person",personMDL.Person),
                             Database.MakeInParam("@Re",personMDL.Re)
                                  }
          int actionID = Database.RunProc("Person_Insert",parms);
          return actionID;
    }
    //DataBase是自己封装的一个对数据库操作的类
      

  10.   

    哈哈,我面试的时候也有这个题,我写的答案是:
    select top 1 id from 表名 order by id desc
    这样写的结果大家可想而知,我被公司录用了!
      

  11.   

    wantaodyc(藏马) 
    如果两个人或者更多的人同时执行insert操作 结果会怎么样?
      

  12.   

    上面我看 lxg13(翔子) 的是唯一正确的。这类问题到 SQL Server 板块去问比较好。
      

  13.   

    用select @@identity是可以得到插入记录的ID值,但我不能保证是不是就是“sp1234(我的女朋友是大美女)”所说的“凑巧碰对了临时的答案”,我跟项目经理说这样可以,他说不提倡这样的写法...
      

  14.   

    @@identity 是返回会话中最后的identity 的 id,实际上当有触发器,或者使用 execute 宏命令,或者一些 DBCC 命令正在工作的时候,或者还有其他一些情况(有两年没有去仔细考虑SQL Server系统问题了),@@identity会返回数据库系统内部操作所经历的最后一个 identity 值,而不是你想要的那个。因此,SQL Server在@@identity函数之外设计有其他的解决办法。在设计业务系统的时候,不要使用int identity定义字段类型。业务数据输出或者移到其他数据库里的时候,其唯一性应该保持,而不应该重新编码。比如你的身份证号码放在本省的户籍库中,当移植到另一省公安厅的库中,仍然应该保持原来的号码,这样相关资料才不致于出现问题。所以业务数据的唯一标识应该使用  GUID 或者类似的不变的编码方式。
      

  15.   

    sp1234(我的女朋友是大美女) 讲的深奥 我不理解哇`~现在数据库表里ID是主键已经定了,改变不了了
    要返回插入记录的ID值请问sp1234(我的女朋友是大美女)有何高见 ?
      

  16.   

    ID是主键没有错呀,对于整个设计、编程没有影响呀。只不过他的类型不应该是 int identity,而应该是uniqueidentifier。
      

  17.   

    很小的差别,只不过插入数据的时候不能依赖数据库去生成下一个 ID (虽然把字段定义为default row()也能自动生成),而应该由业务对象生成它的ID(system.guid.newguid.tostring())。
      

  18.   

    如果你是你们那种定义,你的程序首先select出IDselect isnull(max(id),0)+1 from myTable然后再用这个 ID 作为新业务对象的ID在 Insert 的时候与其它字段一齐写入,那么你的程序在恰好多用户增加记录的时候会经常失败!使用 @@identity 函数会得到错误结果,尽管对于不太善用数据库的人可能只把数据库当作一个简单的数据文件而不会使用触发器、宏命令、约束、default等,这些运行时都会让@@identity函数返回的不是你insert语句插入的那条记录的ID而是它们内部处理过程中最后经历的ID。
      

  19.   

    create table x(id int identity,b nvarchar(30))
    create table y(id int identity,b nvarchar(30))
    go
    create trigger xx on x after insert as
      insert y(b) 
          select cast(getdate() as varchar(30))+'插入了'+
             cast(i.id as varchar(30))
          from inserted as i
    go
    insert y(b) values('开始测试')
    insert x(b) values('测试数据')
    select @@identity as [你输出的@@identity]
    select * from x
    go运行上面这个例子,你会明白看到 @@identity 根本驴唇不对马嘴。在insert执行时数据库很多自动运行的过程、或者函数中(例如触发器、给记录其它字段定义的默认值函数或者约束、自己启动很多监视过程等等),一旦有int identity值,那个值就被返回,而不是你想要的值。
      

  20.   

    你在  
      select * from x
    之后再加上一句
      select * from y
    就看的更清楚了!
      

  21.   

    应该是在 Insert Into 语句成功插入一条记录后,再执行 Select Max(IDENTITYCOL) FROM x 语句获取当前表的最大增量值。select @@identity 是可以,但在网络版系统中就不行啦,因为它是返回当前数据库的最新插入记录的表的最大增量值。假如你使用的是x表,当你执行Insert Into 语句之后,另一个人又使用y表,并也使用了Insert Into 语句,然后你再使用select @@identity的话,你所取得的是y表的最大增量值,而不是x表。不信你可以试试~~  ^_^
      

  22.   

    数据库摆在那里,并不一定只有一支进程/线程去操纵同一个表。写入10号记录,然后再select取出最大id,可能取出的是11号。
      

  23.   

    觉得大家讲的都有道理,我们是这样写的:
    CREATE PROCEDURE dbo.Action_Insert
    @ActionCode char(50),
    @ActionName varchar(50),
    @ID int output
    begin tran
    insert into Action (ActionCode,ActionName) 
    values(@ActionCode,@ActionName)
    set @ID = @@IDENTITY
    if @@error!=0
    begin
    rollback
    end
    else
    begin
    commit
    end
    =========
    一个传出参数@ID,当insert成功执行后,取得这个传出参数的值 。
    这是项目经理的做法 :( ,大家有什么意见啊 ?