February 16, 2017 at 8:57 am
I have a store Procedure which generate output as one random number
I need to execute 100 times how can I do this.
Thanks
Nita
February 16, 2017 at 9:04 am
What do you plan to do with this random number?
February 16, 2017 at 9:12 am
Quick questions, in this context, what is random? Must the output be unique every time? Is there a limit for the values, i.e. 1-1000?
😎
February 16, 2017 at 9:16 am
Here is literally what you've asked for.
CREATE PROCEDURE GenerateOneRandomNumber
AS
SELECT RAND() RandomNumber;
GO
EXEC GenerateOneRandomNumber;
GO 100
"Do not seek to follow in the footsteps of the wise. Instead, seek what they sought." - Matsuo Basho
February 16, 2017 at 9:27 am
ZZartin - Thursday, February 16, 2017 9:04 AMWhat do you plan to do with this random number?
my store procedure ( exec SP_DEV_generateanynumber)
When I execute the above SP I get 3@8 as output
All I want to execute this 100 times in one attmept to get 100 random number
February 16, 2017 at 9:53 am
Nita Reddy - Thursday, February 16, 2017 9:27 AMZZartin - Thursday, February 16, 2017 9:04 AMWhat do you plan to do with this random number?my store procedure ( exec SP_DEV_generateanynumber)
When I execute the above SP I get 3@8 as output
All I want to execute this 100 times in one attmept to get 100 random number
If I understand you correctly, the stored procedure is working as needed, you just want to have it run 100 times.
You can do this using the optional parameter of GO statement to execute T-SQL batch X number of times.
For example:
EXEC SP_DEV_generateanynumber;
GO 100
"Do not seek to follow in the footsteps of the wise. Instead, seek what they sought." - Matsuo Basho
February 16, 2017 at 10:45 am
Nita Reddy - Thursday, February 16, 2017 9:27 AMZZartin - Thursday, February 16, 2017 9:04 AMWhat do you plan to do with this random number?my store procedure ( exec SP_DEV_generateanynumber)
When I execute the above SP I get 3@8 as output
All I want to execute this 100 times in one attmept to get 100 random number
Please note that you are dealing with stored procedures.
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
February 16, 2017 at 11:25 am
Change your proc to access a 'how many?' parameter and then use something like this:
DECLARE @n INT = 10;
SELECT TOP (@n) RAND(CHECKSUM(NEWID()))
FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) a(n)
CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) b(n);
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
February 16, 2017 at 11:35 am
Or my preferred format (so I know how many values I have)
DECLARE @n INT = 10;
SELECT TOP (@n) RAND(CHECKSUM(NEWID()))
FROM (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) a(n)
CROSS JOIN (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) b(n);
February 16, 2017 at 12:11 pm
Steve Jones - SSC Editor - Thursday, February 16, 2017 11:35 AMOr my preferred format (so I know how many values I have)
DECLARE @n INT = 10;
SELECT TOP (@n) RAND(CHECKSUM(NEWID()))
FROM (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) a(n)
CROSS JOIN (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) b(n);
I had to stare at that for at least 5 seconds before I spotted the difference(s)! Nice tweak, I prefer your format too.
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
February 23, 2017 at 2:27 pm
or how about just integers:
DECLARE @n INT = 10;
SELECT TOP (@n) convert(int,RAND(CHECKSUM(NEWID()))*@n)
FROM (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) a(n)
CROSS JOIN (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) b(n);
RegardsRudy KomacsarSenior Database Administrator"Ave Caesar! - Morituri te salutamus."
February 23, 2017 at 2:59 pm
I can't say it's my idea. I'm not sure if this was from Dwain Camps or someone else, but having the numbers there means I can easily do some math. I've had a few cases where I wanted something like 25
SELECT RAND(CHECKSUM(NEWID()))
FROM (VALUES(0),(1),(2),(3),(4),(5)) a(n) -- 5
CROSS JOIN (VALUES(0),(1),(2),(3),(4),(5)) b(n); -- * 5
or 1000
SELECT RAND(CHECKSUM(NEWID()))
FROM (VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9), (0)) a(n) -- 10
CROSS JOIN (VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9), (0)) b(n); -- * 10
CROSS JOIN (VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9), (0)) c(n); -- * 10
and having the numbers listed there helps. Note, I have sometimes moved to the 0 at the end as well.
February 23, 2017 at 3:16 pm
Nita Reddy - Thursday, February 16, 2017 8:57 AMI have a store Procedure which generate output as one random numberI need to execute 100 times how can I do this.
Thanks
Nita
Post the stored procedure. We may be able to help in more ways than 1.
--Jeff Moden
Change is inevitable... Change for the better is not.
February 23, 2017 at 3:49 pm
Jeff Moden - Thursday, February 23, 2017 3:16 PMNita Reddy - Thursday, February 16, 2017 8:57 AMI have a store Procedure which generate output as one random numberI need to execute 100 times how can I do this.
Thanks
NitaPost the stored procedure. We may be able to help in more ways than 1.
In addition, what do you want to do with the output of the procedure? I mean other than just generate 100 random numbers.
February 23, 2017 at 3:57 pm
Steve Jones - SSC Editor - Thursday, February 23, 2017 2:59 PMI can't say it's my idea. I'm not sure if this was from Dwain Camps or someone else, but having the numbers there means I can easily do some math. I've had a few cases where I wanted something like 25
SELECT RAND(CHECKSUM(NEWID()))
FROM (VALUES(0),(1),(2),(3),(4),(5)) a(n) -- 5
CROSS JOIN (VALUES(0),(1),(2),(3),(4),(5)) b(n); -- * 5
or 1000
SELECT RAND(CHECKSUM(NEWID()))
FROM (VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9), (0)) a(n) -- 10
CROSS JOIN (VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9), (0)) b(n); -- * 10
CROSS JOIN (VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9), (0)) c(n); -- * 10and having the numbers listed there helps. Note, I have sometimes moved to the 0 at the end as well.
I usually use either a Tally Table or a Tally function instead of hard coding a given return size. That way, I only need to change a parameter instead of having to change code every time someone changes the requirements. Even if I weren't allowed to have a Tally Table or Tally function, I'd write the row generator as a set of cascading CTEs and still use a parameter. IIRC, several people have written books for RedGate on the subject of bullet proofing your code and, maybe it's just me, I consider that a form of bullet proofing.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 15 (of 16 total)
You must be logged in to reply to this topic. Login to reply