<cfloop condition="true">
<cfset job_number = randRange(1, 100000) />
<cfquery name="checkopen">
SELECT count(job_number)
FROM openjobs
WHERE job_number = #job_number#
</cfquery>
<cfquery name="checkclose">
SELECT count(job_number)
FROM closejobs
WHERE job_number = #job_number#
</cfquery>
<cfif checkopen.recordcount EQ 0 AND checkclose.recordcount EQ 0>
<cfbreak />
</cfif>
</cfloop>
In english:
start an infinite loop
pick a number
see if it exists in open
see if it exists in closed
if it exists in neither, we've found a winner, so break, otherwise start the
loop over again.
Keep in mind that this will work fine for the first few thousand records,
but after that it'll start to get increasingly slower, as more collisions
occur. Is there a good reason you can't just use the DB to create an
auto-increment (identity) field on openjobs, which you'd then copy to close
jobs when it is closed? that'd be a LOT more efficient.
barneyb
> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> Sent: Thursday, March 20, 2003 9:56 AM
> To: CF-Talk
> Subject: Unique random number?
>
>
> Need to set a job number to a unique number, based of creating a number
> using the RandRange function, but then I need to take that number and
> bounce it off two access tables (called openjobs & closejobs) to see if
> that random number is already used, then loop back if it is to start over,
> but if it isn't, then use it to create a new record in openjobs table.
>
> What I don't know how to do is the loop structure if the number already
> exists on one of the two tables.
>
> Could someone direct me in how the code would look starting with
> this CFSET
> statement?
>
> <CFSET job_number = RandRange(1, 100000)>
>
> ...and as always, thanks in advance!
>
>
>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4
Subscription:
http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
This list and all House of Fusion resources hosted by CFHosting.com. The place for
dependable ColdFusion Hosting.
Unsubscribe:
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4