How to write a stored procedure in SQL Server 2008 that generates 3 million random no

How to write a stored procedure in SQL Server 2008 that generates 3 million random numbers in two columns in an integer data type.

+3
source share
2 answers
SELECT TOP 3000000 
    ABS(CHECKSUM(NewId())) As RndCol1, 
    ABS(CHECKSUM(NewId())) AS RndCol2
FROM 
    sys.objects s1 
    CROSS JOIN sys.objects s2 
    CROSS JOIN sys.objects s3
    CROSS JOIN sys.objects s4

[You can check the actual distribution of these numbers using the chi-square test]

UPDATE . The randomness of these events may not meet the chi-square distribution criterion. I would advise testing in your circumstances.

+5
source

You can use CTE, for example:

create procedure dbo.GiveMeRandomNumbers
as
    begin
    with qry as (
        select  CAST(CAST(NEWID() AS VARBINARY) AS INT) as col1
        ,       CAST(CAST(NEWID() AS VARBINARY) AS INT) as col2
        ,       0 as i
        union all
        select  CAST(CAST(NEWID() AS VARBINARY) AS INT) as col1
        ,       CAST(CAST(NEWID() AS VARBINARY) AS INT) as col2
        ,       i + 1 
        from    qry
        where   i < 3000000
    )
    select  col1, col2
    from    qry
    option  (maxrecursion 0)
    end

newid, rnd CTE.

+1

Source: https://habr.com/ru/post/1749792/


All Articles