ecojohnson
Programmer
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.
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.