Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Why cant pass value by reference in WHERE clause?
Message
From
24/07/2001 11:38:01
 
 
To
24/07/2001 04:27:14
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
00534258
Message ID:
00534436
Views:
25
John,
In addition to what Larry said, beware that there's a trap when using UDFs from within SQL SELECT. If you initialize a memvar as Larry suggested, you will get an unexpected result because the VFP SQL parser evaluates the UDF before running it. This causes the first record to be evaluated twice. Here's a simple example the demonstrates this behaviour:
USE "C:\Program Files\Microsoft Visual Studio\MSDN98\98VS\1033\Samples\VFP98\Tastrade\Data\CUSTOMER.DBF"

lnI = 0
SELECT sqlTest(), RECNO() FROM customer
FUNCTION sqlTest
lnI = lnI+1
RETURN lnI
As Larry suggested, you could use private memvars and update the memvars from your UDF. The example below calculates a running total of the discount field. Note that the UDF skips the first call in order to get the correct result:
lnSum = 0
lnCount = 1
*-- calculate a running total of the discount field
SELECT sqlTest(discount), discount FROM customer
FUNCTION sqlTest
LPARAMETER tnValue

*-- UDF that allows calculating running totals.
*-- Adds tnValue to lnSum.
*-- Assumes that the following variables are inititalized before
*-- the SQL SELECT command:
*-- PRIVATE lnCount, lnSum
*-- lnCount = 1
*-- lnSum   = 0

IF lnCount > 1
	lnSum = lnSum + tnValue
ELSE
	*-- skip first call (called by SQL parser)
	lnSum = 00000000.00
ENDIF

lnCount = lnCount + 1

RETURN lnSum
>Hi,
> I have a UDF funtion which will be called in my SQL statement - WHERE clause. I need ot pass a variable by reference to this UDF funcion. However, it doesn't work. The variable doesn't passed by reference (I try to change the value in UDF function by the value reset on next call). It works if i call it from HAVING clause. Any ideas?
>
>Thank you
>
>
>
>SELECT * FROM csrInvoice ;
>WHERE THIS.Include(Qty, StockQty, @lnCount, @lnSum) ;
>INTO CURSOR csrInvoice
>
>
Daniel
Previous
Reply
Map
View

Click here to load this message in the networking platform