PRB: SQL SELECT Statement UDF Executes Twice On First RecordLast reviewed: July 31, 1995Article ID: Q133001 |
The information in this article applies to:
SYMPTOMSA user-defined function (UDF) called as a select item in an SQL SELECT statement executes twice for the first record of the source table.
CAUSEThe first iteration of the SQL SELECT statement creates and sizes the columns in the output. The first iteration therefore executes once to do this and once to determine what data to return from the first record.
RESOLUTIONInsert code in the UDF to suppress its execution on the first iteration. Please see the examples in the More Information section.
STATUSThis behavior is by design.
MORE INFORMATION
Steps to Reproduce Behavior
SQLUDF.PRG Notes
Example WorkaroundThe following program is a modification of the above code. It suppresses the extra calculation for the first iteration of the UDF:
* start of SQLUDF2.PRG * * This program sets a logical flag to test for the first iteration of * the UDF to suppress getting a sum from the first record twice. * nRunningTot = 0 && initialize running total to 0 nIterations = 0 && initialize iteration counter to 0 lFirst_Time = .T. && initialize 1st time through variable SELECT *, Add_Em(nField1) as nRunTotal FROM SQLUDF INTO CURSOR temp BROWSE NOWAIT PROCEDURE Add_Em PARAMETERS tnFld1 IF lFirst_Time != .T. && Only run if not 1st time through nIterations=nIterations + 1 && Increment iteration counter WAIT WINDOW "Record Number: " + ; ALLTRIM(STR(RECNO())) + ; CHR(13) +"Number of Iterations: " + ALLTRIM(STR(nIterations)); TIMEOUT 1 nRunningTot = nRunningTot + tnFld1 && Calculate running total ENDIF lFirst_Time=.F. && Togggle to .F. on 1st call to UDF RETURN nRunningTot ENDPROCEDURE *End of SQLUDF.PRG Workaround Notes
General Notes
REFERENCESFor more information about a similar behavior of a UDF, please see the following article in the Microsoft Knowledge Base:
ARTICLE-ID: Q124402 TITLE : Calling a User-Defined Function (UDF) from SQL SELECT |
Additional reference words: 3.00 2.60a VFoxWin FoxMac FoxDos FoxWin
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |