Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populate random data into a table 1

Status
Not open for further replies.

ecojohnson

Programmer
Jul 2, 2001
54
0
0
US
Hi everyone.

I am trying to create a table that has two columns, column1 and column2. I want a loop so that column1 starts with 1, and end with 2000 (counting up by 1). I want column2 to have a random number between 1 and 999.

I have each of these working separately. For column1, I have:

DECLARE @i INT
SELECT @i = 1
WHILE @i <= 10
BEGIN
INSERT INTO table (column1) SELECT @i
SELECT @i = @i + 1
END

For column2, I have:

DECLARE @Random int;
DECLARE @Upper int;
DECLARE @Lower int
SET @Lower = 1 -- The lowest random number
SET @Upper = 999 -- The highest random number
SELECT @Random = Round(((@Upper - @Lower -1) * Rand() + @Lower), 0)
SET NOCOUNT ON
INSERT INTO agetest (age)
VALUES (@Random)
SET NOCOUNT OFF
GO

How do I combine these statements to work as one, so that my rows are return something like:

1 303
2 712
3 48
4 117

Thanks.
 
Code:
SET NOCOUNT ON

Declare @Temp Table(Column1 Int Identity(1,1), Column2 Int)
DECLARE @Random int;
DECLARE @Upper int;
DECLARE @Lower int

SET @Lower = 1 -- The lowest random number
SET @Upper = 999 -- The highest random number

DECLARE @i INT
SELECT @i = 1
WHILE @i <= 2000
BEGIN
  INSERT INTO @Temp (Column2) Values(Round(((@Upper - @Lower -1) * Rand() + @Lower), 0))
  Set @i = @i + 1
END

Select * from @Temp


-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Hi gmmastros

Thanks for the tip. Here's a star.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top