• Christian Buettner (2/13/2008)


    Um, question:

    Shouldn't it be executing once per row in the FROM clause too?

    Example:

    SELECT * FROM dbo.MyTable T CROSS APPLY dbo.MyFunction(T.fielda)

    Grazias!

    I think in this context the CROSS APPLY clause isn't counted as part of the FROM!

    In http://msdn2.microsoft.com/en-us/library/ms175156.aspx (quoted by Christian above) Microsoft says that the UDF in the APPLY will execute once per row of the 'left input'.

    Elsewhere, http://msdn2.microsoft.com/en-us/library/aa175085(SQL.80).aspx, it is indicated that the exact number of executions may be difficult to determine.

    The number of times that a function specified in a query is actually executed can vary between execution plans built by the optimizer. An example is a function invoked by a subquery in a WHERE clause. The number of times the subquery and its function is executed can vary with different access paths chosen by the optimizer.

    About the only certain thing is thatSELECT ... FROM udfwill execute the UDF once, and a UDF anywhere else will probably be executed at least once for each row returned, but may be executed many more times than that!

    Derek.

    Derek