CREATE FUNCTION RandNumber
(@intLength int)
returns varchar(100)
as
begindeclare @strSeed  varchar(100)
declare @seedLength int
declare @strs varchar(100)
declare @rad int
declare @count intset @strSeed='ab1c4d6e7fghiI0JK7LMN9OPQRS3T9UVWX8YZjkl2mn7opqr4stu86vwxy5zABCD6EFGH'
set @seedLength=len(@strSeed)
set @strs=''
set @count=1while @count<=@intLength
  begin
    @rad=select cast(ceiling(rand() * @seedLength) as int);
    @strs=@strs+substring(@strSeed,@rad,1);
  end
return @strs
end
一个卡密生成函数,不知道哪里错了,大家帮帮忙

解决方案 »

  1.   

    CREATE FUNCTION RandNumber
    (@intLength int)
    returns varchar(100)
    as
    begindeclare @strSeed varchar(100)
    declare @seedLength int
    declare @strs varchar(100)
    declare @rad int
    declare @count intset @strSeed='ab1c4d6e7fghiI0JK7LMN9OPQRS3T9UVWX8YZjkl2mn7opqr4stu86vwxy5zABCD6EFGH'
    set @seedLength=len(@strSeed)
    set @strs=''
    set @count=1while @count<=@intLength
      begin
      set @rad=(select cast(ceiling(rand() * @seedLength) as int));
      set @strs=@strs+substring(@strSeed,@rad,1);
      end
    return @strs
    end
      

  2.   

      set @rad=(select cast(ceiling(rand() * @seedLength) as int));
      set @strs=@strs+substring(@strSeed,@rad,1);
      

  3.   

    还是不行,出现 invalid use of side-effecting or time-dependent operator in “rand” within a function
      

  4.   

    #1/#2 没留意代码里面有rand()
      

  5.   

    rand()是非确定性函数,而在用户定义函数中只能调用确定性函数。
      

  6.   

    agree #6函数中一定要使用非确定性函数也是有办法的:create view vrand --视图
    as
    select rand() rd
    gocreate function fn_test()
    returns float as
    begin
    return(select rd from vrand) --调用视图
    end
    goselect dbo.fn_test()