create function GetNewMemberId()   
returns int
as
begin
      declare @tmp int
      select @tmp=max(idtype) from IdGenerator      return isnull(@tmp+1,1) 
end
go
create function GetNewProductId() 
returns int
as
begin
      declare @tmp int
      select @tmp=max(idvalue) from IdGenerator      return isnull(@tmp+1,1) 
end
go

解决方案 »

  1.   

    create function GetNewMemberId()   
    returns int
    as
    begin
          declare @tmp int
          select @tmp=max(idtype) from IdGenerator with(rowlock)      return isnull(@tmp+1,1) 
    end
    go
    create function GetNewProductId() 
    returns int
    as
    begin
          declare @tmp int
          select @tmp=max(idvalue) from IdGenerator with(rowlock)      return isnull(@tmp+1,1) 
    end
    go
      

  2.   

    谢谢   vivianfdlpw() 有二个问题想问一下:一个是更新操作应该在哪儿执行?
    还有一个是在Select时函数加了行锁,在何时解锁的呢?  在函数执行完毕的时候自动解锁的吗?
      

  3.   

    还有一个是在Select时函数加了行锁,在何时解锁的呢?  在函数执行完毕的时候自动解锁的吗?======>行锁,如果是单独的就在行锁的语句运行完解锁的;如果是在事物里执行,就在提交事回滚事物事解锁。
      

  4.   

    还有一个是在Select时函数加了行锁,在何时解锁的呢?  在函数执行完毕的时候自动解锁的吗?======>行锁,如果是单独的就在行锁的语句运行完解锁的;如果是在事物里执行,就在提交事物或回滚事物时解锁。
      

  5.   

    没看清你的意思,函数内不允许使用update,如果你只想返回下一个值:create function GetNewMemberId()   
    returns int
    as
    begin
          declare @tmp int
          select @tmp=max(idtype) from IdGenerator with(rowlock)
          where idtype='MemberId'      return isnull(@tmp+1,1) 
    end
    go
    create function GetNewProductId() 
    returns int
    as
    begin
          declare @tmp int
          select @tmp=max(idvalue) from IdGenerator with(rowlock)
          where idtype='ProductId'      return isnull(@tmp+1,1) 
    end
    go
      

  6.   

    哦,谢谢,  
    我不知道SQL函数内是不能使用update的,那我原来的思路就有问题了我原本的思路是 
    列     idtype       idvalue
    记录   'memberid'    1
    每调用一次GetNewMemberId()函数, idvalue的值就增加1
    不过这样做的话就要用到Update语句了。请问vivianfdlpw() 一下,你给的代码里是如何确保函数返回值是不断增长的呢?
    我好像没有看到改变idvalue的代码?另外想再问一下, 在函数内是无法执行Update操作的,那能不能执行 Insert? Delete? 事务?
      

  7.   

    create function GetNewMemberId()
      returns int
    as begin
      declare @id int
      
      begin tran
      update IdGenerator 
      set @id=idvalue =idvalue +1 
      where idtype ='MemberId'
      
      if @@rowcount<>1 or @@error<>0
      begin
        rollback tran
        raiserror( '更新失败' ,16,1) 
      end 
      else 
        commit tran
      return @id
    end   --------------------------------
    未考虑没有记录时情况,
      

  8.   

    create function GetNewProductId() 
    returns int
    as
    begin
          declare @tmp int
          select @tmp=max(idvalue) from IdGenerator with(rowlock)
          where idtype='ProductId'      return isnull(@tmp+1,1) 
    end
    go
      

  9.   

    --如果采用存储过程应该就比较方便了./* 创建存储过程 */alter proc dbo.GetNewMemberId
    @MemberId int output
    asupdate Test with (Rowlock)set idvalue=idvalue+1,
        @MemberId=idvalue
    where idtype='MemberId'Go
    /* 执行存储过程,在程序里边就可以获得存储过程返回的值 */declare @a int
    exec dbo.GetNewMemberId @a output
    select @a