Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Random Numbers

Author  Topic 

elev8
Starting Member

4 Posts

Posted - 2008-12-15 : 21:19:17
I'm trying to add a new value for each row in my database, im very new to sql server. can anyone explain why this wont work and offer a possible solution please. TYIA


counter = '0'

start:
if counter=957
begin
goto the_end;
end
else
begin
select
set price = (round((rnd * 600) + 1))
counter+=1
goto start;
end

the_end:
select price, * from stock

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-15 : 21:49:03
Your syntax isn't valid T-SQL. Are you using SQL Server or some other database platform?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

elev8
Starting Member

4 Posts

Posted - 2008-12-15 : 22:03:08
Im using sql server, and the code has now changed to;


DECLARE @r INTEGER
SET @r = 1
WHILE @r < 958
BEGIN
UPDATE Stock
SET
Price = ROUND((RAND(100) * 1000), 2)
WHERE Recorder = @r

SET @r = @r + 1
END


but still each row is coming up as the same value
Go to Top of Page

elev8
Starting Member

4 Posts

Posted - 2008-12-15 : 22:54:45
I've finally come up with 2 solutions


DECLARE @random integer;
SET @random = 1;
WHILE @random < 958
BEGIN
UPDATE Stock
SET
Price = ROUND((RAND() * 600), 0)
WHERE Recorder = @random
SET @random = @random + 1
END;





DECLARE @r INTEGER;
DECLARE @t INTEGER;
DECLARE @j INTEGER;
SET @r = 1SET @j = 600

WHILE @r < 958
BEGIN


UPDATE Stock
SET
Price = ROUND((RAND() * @j), 0)
WHERE Recorder = @r

SET @r = @r + 1
SET @j = @j - 1
END
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-15 : 23:33:59
whats the purpose of loop? cant you go for batch update? are you just trying to random update some records?
Go to Top of Page

elev8
Starting Member

4 Posts

Posted - 2008-12-17 : 01:54:44
On the contrary, I was trying to update each row with some random numbers. I had previously set them, all to zero without backing up.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-17 : 02:03:13
quote:
Originally posted by elev8

On the contrary, I was trying to update each row with some random numbers. I had previously set them, all to zero without backing up.


even then wats the purpose of loop? cant you just fire a batch update on all records with value 0 with random number?

Go to Top of Page
   

- Advertisement -