December 12, 2004 at 3:58 am
thanks
Dagaz
Code------>
Randomize()
Set RSFormUpd = Server.CreateObject("ADODB.Recordset")
RSFormUpd.open "Select * From ViewWeeklyFormUpdate", Conn, 3, 3
Do While Not RSFormUpd.EOF
UpdForm = Round((int(RSFormUpd("StableHands"))/22) + (rnd * 2),0)
If UpdForm = 0 Then
If int(RSFormUpd("Form")) - 1 = 0 Then
RSFormUpd("Form") = int(RSFormUpd("Form"))
Else
RSFormUpd("Form") = int(RSFormUpd("Form")) - 1
End If
End If
If UpdForm = 1 Then
RSFormUpd("Form") = int(RSFormUpd("Form"))
End If
If UpdForm = 2 Then
If int(RSFormUpd("Form")) + 1 = 6 Then
RSFormUpd("Form") = int(RSFormUpd("Form"))
Else
RSFormUpd("Form") = int(RSFormUpd("Form")) + 1
End If
End If
Response.write RSFormUpd("Form") & " "
RSFormUpd.update
RSFormUpd.movenext
Loop
RSFormUpd.close
Set RSFormUpd = Nothing
in pseudo code
Form from Horses
StableHands from owners
linked by Horses.OwnedBy = Owners.OwnerID
ok goes through all the horses and creates a number either 0,1,2
based on the formula
Round((int(RSFormUpd("StableHands"))/22) + (rnd * 2),0)
then if the random number = 0 the Form goes down 1 unless is already 1 then stays the same
if = 1 then stays same
if = 2 then form goes up 1 unless it is already 5 then it stays the same
December 13, 2004 at 12:41 am
Put your stuff in a stored procedure which shouldn't be too difficult at all. Create then a job and schedule it for your needs.
And, btw, you should really use the ADO command object.
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
December 13, 2004 at 1:23 am
December 13, 2004 at 1:31 am
Something like the following should work:
Create Procedure usp_my_procedure As
Update ViewWeeklyFormUpdate
Set Form =
Case Round( cast( StableHands as int)/22) + (rand() * 2),0)
When 1 Then Cast(Form as Int)
When 2 Then
Case
When (Form+1)=6 Then Cast(Form as Int)
Else Cast(Form as Int) -1
End
Else 0
End
You may need to replace the view with it's constituent parts.
Regards,
Nic Washington
December 13, 2004 at 1:37 am
Don't suppose you could do a simple example?
I could, but Nic has done so already.
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
December 13, 2004 at 1:38 am
December 13, 2004 at 12:28 pm
lol ok got home and definate improvement. my only question is that rand seems to be the same rand for all records? am i wrong in this or is there another setting i need to change
December 13, 2004 at 1:07 pm
Case
Else Cast(Form as Int) +1
Case
Else Cast(Form as Int) -1
December 13, 2004 at 7:51 pm
The RAND function only executes once per query...no matter how you write it all rows will have the same result.
The common way to randomize in T-SQL is to use the NEWID() function which generates a unique random value for each row. Order by this and select the desired count or percent off the top.
When converting row-by-row procedural logic to T-SQL you have to change your approach to set-based rather than row-based.
Your problem involves different probabilities for different groups, so there are multiple random selections in my solution:
-- Fewer than 22 stablehands: 1/3 decrement (1 min), 1/3 increment (5 max)
-- 22-43 stablehands: 2/3 increment (5 max)
-- 44+ stablehands: all increment (5 max)
create table #decrement (HorseID int not null primary key clustered)
-- Determine which horses will be decremented
-- 33% of (StableHands < 22)
insert into #decrement
select top 33 percent HorseID
from ViewWeeklyFormUpdate
where StableHands < 22
order by newid()
update ViewWeeklyFormUpdate set Form = Form - 1
where Form > 1 and HorseID in (select HorseID from #decrement)
-- Determine which horses will be incremented
-- 50% of (StableHands < 22) that were not decremented
-- 67% of (22 <= StableHands < 44)
-- All StableHands >= 44
update ViewWeeklyFormUpdate set Form = Form + 1
where Form < 5 and (StableHands >= 44 or HorseID in (
select top 50 percent HorseID from ViewWeeklyFormUpdate
where StableHands < 22 and HorseID not in (select HorseID from #decrement)
order by newid()
union all
select top 67 percent HorseID from ViewWeeklyFormUpdate
where (StableHands between 22 and 43)
order by newid()))
drop
table #decrement
December 14, 2004 at 8:42 am
Hi Guys,
I know there will be loads of abuse for this but had to do it.........
You can make rand work on multiple occaisions in one sp - by using a bit of dynamic sql and sp_executesql e.g.:-
DECLARE @RandomisedNumber decimal,
@SQL nvarchar(4000),
@Counter decimal
SET @Counter = 1
WHILE @Counter < 10
BEGIN
SET @sql = N'SET @RandomisedNumber = round(' + CAST(@Counter AS Varchar(255)) + '/22 + (rand() * 2),0)'
EXEC sp_executesql @sql, N'@RandomisedNumber decimal OUTPUT', @RandomisedNumber OUTPUT
PRINT CAST(@RandomisedNumber as varchar(255))
SET @Counter = @Counter + 1
END
Have fun
Steve
We need men who can dream of things that never were.
December 14, 2004 at 12:59 pm
DECLARE @sql NVARCHAR(500)
SET @sql = N'SET @RandomisedNumber = (rand() * 2)'
EXEC sp_executesql @sql, N'@RandomisedNumber decimal OUTPUT' ,@RandomisedNumber OUTPUT
Case
Else Cast(Form as Int) +1
Case
Else Cast(Form as Int) -1
December 14, 2004 at 1:52 pm
Here's an idea...
CREATE TABLE [Numbers] (
[PkNumber] [int] IDENTITY (1, 1) NOT NULL ,
CONSTRAINT [Pk_Number] PRIMARY KEY CLUSTERED
(
[PkNumber]
) ON [PRIMARY]
) ON [PRIMARY]
GO
Declare @i as int
set @i = 0
while @i < 8000 -- stop whenever you want but the bigger this table is the longer it'll take to rurn queries from it.
begin
Insert into dbo.Numbers Default Values
set @i = @i + 1
end
set @i = 0
while @i < 5
begin
Select top 10 PkNumber, PkNumber % 2 as mod from dbo.Numbers order by Newid()
set @i = @i + 1
end
If you run this query as a subquery you'll get a different rand everytime.
December 15, 2004 at 3:13 am
Hi,
Example with your existing code:-
Update ViewWeeklyFormUpdate
Declare @sql nvarchar(4000),
@RandomisedNumber decimal
Set Form =
SET @sql = N'SET @RandomisedNumber = rand() * 2'
EXEC sp_executesql @sql, N'@RandomisedNumber decimal OUTPUT', @RandomisedNumber OUTPUT
Case round( cast( StableHands as int)/22 + CAST(@RandomisedNumber as varchar(255)),0)
When 1 Then Cast(Form as Int)
When 2 Then
Case
When (Form+1)=6 Then Cast(Form as Int)
Else Cast(Form as Int) +1
End
Else
Case
When (Form-1)=0 Then Cast(Form as Int)
Else Cast(Form as Int) -1
End
End
Hope it helps.
Have fun
Steve
We need men who can dream of things that never were.
December 15, 2004 at 3:51 am
December 15, 2004 at 12:11 pm
Incorrect syntax near the keyword 'Declare'.
Server: Msg 170, Level 15, State 1, Line 6
Line 6: Incorrect syntax near '='.
Server: Msg 156, Level 15, State 1, Line 11
Incorrect syntax near the keyword 'Case'.
Viewing 15 posts - 1 through 15 (of 26 total)
You must be logged in to reply to this topic. Login to reply