Article ID: 139048 - Last Review: February 21, 2005 - Revision: 3.4 Update SQL works incorrectly with RAND() function in WHERE clauseThis article was previously published under Q139048 On This PageSYMPTOMS
Using the RAND() in an SQL UPDATE's WHERE clause can cause random results
to occur.
CAUSE
The value of RAND() changes each time it is evaluated, unlike most FoxPro
functions. FoxPro's internal SQL parser evaluates it once to do the
Rushmore optimization and once more as each filter condition is tested for
each record. This causes unpredictable, random results.
RESOLUTION
Instead of using the RAND() function in the WHERE clause, assign the value
of the RAND() function to a memory variable. Then use the variable in the
WHERE clause. For an example, please see the "Code to Correct Problem"
section of this article.
A new RAND() can be generated for every record tested in the filter by putting RAND() in a User Defined Function (UDF) and calling the UDF in the SQL command in place of RAND(): MORE INFORMATIONCode to reproduce the problemThe following code illustrates the problem. If there wasn't a problem, the code should make 1000 updates to the table. Instead, the update fails either 999 or 1000 times.Code to correct the problemTo fix the problem, remove the '*' from the updval line and substitute 'updval'for 'INT(RAND() * 1000 + 1)' of the UPDATE's WHERE clause. The new line would look like the following:APPLIES TO
| Article Translations
|

Back to the top
