Partager via


Today's Quiz. How many times is a user function called?

Run the code below. How many times is the function FOO called? Why?

The first SELECT is a single table query. The next is a 2 table self-join with no join condition.

PUBLIC nCount

nCount=0

CREATE CURSOR test (name c(10))

nRecs=5

FOR i = 1 TO nRecs

      INSERT INTO test VALUES ("test"+TRANSFORM(i))

ENDFOR

SELECT name,foo(name) AS foo FROM test INTO CURSOR result

?"Count = ",nCount

nCount=0

CLOSE DATABASES all

nCount=0

CREATE CURSOR test (name c(10))

FOR i = 1 TO nRecs

      INSERT INTO test VALUES ("test"+TRANSFORM(i))

ENDFOR

SELECT a.name,foo(a.name) AS foo FROM test a, test b INTO CURSOR result

?"Count = ",nCount

PROCEDURE foo(cName)

      ?PROGRAM(),cName

      nCount=nCount+1

      RETURN nCount

RETURN

Comments

  • Anonymous
    April 10, 2006
    I think, select makes one call to determine the structure of the resluting cursor and then one call for each row in the result.
  • Anonymous
    April 10, 2006
    Coverage Profiler returned 32 hits.
  • Anonymous
    April 12, 2006
    The first call appears to use the first record value to determine the field type and size in order to have the field structure for the resulting query.

    I've found that if I forget to pad a function call to the largest desired width, I end up with truncated values for many of the records in my query result.
  • Anonymous
    April 13, 2006
    The comment has been removed
  • Anonymous
    June 21, 2006
    First one 5 times, one for each record

    Second one 25 times.  Each record of test a is joined with each record of test b, so 5 * 5 = 25.  Function is called once for each record produced.
  • Anonymous
    February 16, 2007
    1: once to determine structure for cursor and once for each record = 6 total 2: once to determine structure for cursor and 25 times for unjoined tables = 26