SQL Server: how to insert random integers into table?

Viewed 43463

I have a test table like this

CREATE TABLE #temp (
    rid INT IDENTITY (1, 1) NOT NULL,
    val INT NULL,
    CONSTRAINT pk_temp_rid PRIMARY KEY (rid)
);

What are the different methods in SQL Server to insert random integers into this table (e.g for 1000 rows). While loop, cross join or what?

I tried this but the result is not correct

DECLARE @val AS INT = 1;

WHILE @val <= 1000
    BEGIN
        INSERT #temp (val)
        SELECT RAND(@val);
        SET @val = @val + 1;
    END

SELECT *
FROM   #temp;
4 Answers
Related