Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Dear Experts,


I need to re-populate a table I am working with Random Numbers in

SQL
DECLARE @RRANDOM BIGINT;
DECLARE @UPPER BIGINT;
DECLARE @LOWER BIGINT;

----This will create a random number

SET @LOWER = 1 
SET @UPPER = 9999999999999
SELECT @RRANDOM = ROUND(((@UPPER-@LOWER-1)*RAND()+@LOWER),0)

SET @RANDOM =	@RRANDOM



The structure of the table is as follows

Idno Ticket_Number(Random Nos) 
-----------------------------------
001 
002
003


Please assist with a Loop and and UPDATE

What I have tried:

Work in progress reviewed some of my codes to no avail
Posted
Updated 4-Feb-18 7:04am
v2
Comments
Patrice T 28-Jan-18 10:38am    
'reviewed some of my codes to no avail'
show what you did and explain problem.
OriginalGriff 28-Jan-18 11:32am    
What have you tried?
Where are you stuck?
What help do you need?

Use the "Improve question" widget to edit your question and provide better information.
CHill60 28-Jan-18 11:56am    
Don't use a loop.
David_Wimbley 28-Jan-18 12:09pm    
Responses like these are as helpful as questions of "I get an error".

What would you use if not a loop? Would be more helpful to OP rather than just saying not to use a loop.
CHill60 28-Jan-18 14:33pm    
I agree. But the OP has made little effort and has not responded to earlier requests for more information.
There are many constructs that can and should be used in preference to loops in set - based languages...too many to list.
I'd post a link to a CP article on the subject but that would just be advertising

1 solution

I'd suggest to read this: sqlauthority.com: SQL SERVER – Random Number Generator Script – SQL Query[^]...
and this: sql server - How do I generate random number for each row in a TSQL Select? - Stack Overflow[^] which refer to: Less Than Dot - Blog - SQL Server – Set based random numbers[^]

I believe that below query will resolve your issue:
SQL
SELECT Idno, CONVERT(BIGINT, @LOWER+(@UPPER-@LOWER)*RAND()) As TicketNumber
FROM YourTable
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900