Update SQL works incorrectly with RAND() function in WHERE clause (139048)
The information in this article applies to:
- Microsoft Visual FoxPro for Windows 3.0
- Microsoft Visual FoxPro for Windows 3.0b
- Microsoft Visual FoxPro for Windows 5.0
- Microsoft Visual FoxPro for Windows 5.0a
- Microsoft Visual FoxPro for Windows 6.0
- Microsoft Visual FoxPro for Windows 7.0
- Microsoft Visual FoxPro 8.0
- Microsoft Visual FoxPro 9.0 Professional Edition
This article was previously published under Q139048 SYMPTOMS
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():
SELECT * FROM <table> WHERE RAND() > .5
In this example the article's CAUSE section applies:
SELECT * FROM <table> WHERE myudf() > .5
PROCEDURE myudf()
RETURN RAND()
Modification Type: | Major | Last Reviewed: | 2/21/2005 |
---|
Keywords: | kbBug kbProgramming KB139048 kbAudDeveloper |
---|
|