CREATE PROCEDURE [dbo].[spCreateRandom]
    @Lower NUMERIC ,
    @Upper NUMERIC ,
    @StartDate SMALLDATETIME ,
    @EndDate SMALLDATETIME ,
    @Random NUMERIC  OUTPUT
AS   
    SET @Random = ROUND(( ( @Upper - @Lower - 1 ) * RAND() + @Lower ), 0)
    IF(EXISTS(
    SELECT  AutoId 
    FROM    dbo.Tbl_RandomLottery
    WHERE   CONVERT(VARCHAR(10), StartDate, 111) >= @StartDate
            AND CONVERT(VARCHAR(10), EndDate, 111) <= @EndDate
            AND Flag = '1'
            AND Id_Havaleh = @Random
            ))
            
        EXECUTE spCreateRandom  @Lower , @Upper , @StartDate , @EndDate  , @Random = @Random OUTPUT                                                
    RETURN @Random
