Use RAND() in User Defined Function

后端 未结 5 996
青春惊慌失措
青春惊慌失措 2020-12-06 06:10

I am trying to create a user defined function which calls the system RAND() function inside it, when I try to create the function it errors out with the followi

5条回答
  •  半阙折子戏
    2020-12-06 06:47

    Just pass RAND() value as parameter from outside:

    CREATE FUNCTION getNumber(@_id int, @RAND FLOAT)
    RETURNS DECIMAL(18,4)
    AS
    BEGIN
       DECLARE @RtnValue DECIMAL(18,4);
    
       SELECT TOP 1 @RtnValue = EmployeeID 
       FROM dbo.Employees
       ORDER BY EmployeeID DESC
    
       SET @RtnValue = @RAND * @RtnValue * (1/100)
    
       RETURN @RtnValue;
    END
    

    And call as getNumber(10, RAND())

    No any side effects.

提交回复
热议问题