Groups | Blog | Home
all groups > sql server (alternate) > october 2006 >

sql server (alternate) : user defined funcyions in sql server 2000


shark
10/14/2006 5:27:33 AM
hi,

it might be a very stupid question but i want to know whether an
execution plan is created in user defined functions in sql server 2000
like stored procedures.

Thanks.
Hugo Kornelis
10/14/2006 11:11:27 PM
[quoted text, click to view]

Hi shark,

Not a stupid question at all, and not a simple answer either. That's
becuase it depends on the type of function.

* Scalar function.
Has an execution plan. Note that the function is executed once for each
row in the calling query's result set, so the execution plan will be
executed many times.

* Multistatement table-valued function.
Has an execution plan. The function is called once per query in which it
is used and the results are materialized in a temporary table that is
then used in the calling query.

* Inline table-valued function.
Has NO execution plan. If a query refers to an inline TVF, the name of
that function is replaced with the definition before the calling query
is optimized (just as with a view). This allows the optimizer to pull
tricks that would otherwise be impossible. But the result is, of course,
that an execution plan is built for the calling query, not for the
function itself.

--
shark
10/15/2006 11:37:32 AM

[quoted text, click to view]

Thanks Hugo . but then what id functiond do have an execution plan wht
are the advantages of using them over stored procedures.
Hugo Kornelis
10/16/2006 12:03:40 AM
[quoted text, click to view]

(snip)
[quoted text, click to view]

Hi Shark,

User-defined functions and stored procedures are used in so different
situations and have such different properties that the choice which one
to use is made based upon how it will be used, not whether an execution
plan gets generated.

I don't know of any situations where you could easily change a stored
procedure to a function or vice versa.

--
Razvan Socol
10/17/2006 2:33:54 AM
Hi, shark

As Hugo said, the choice to use SP-s or UDF-s should be made based upon
how it will be used and what it should do. My rules are the following:
1. If it must change data, then use a SP, else proceed to step 2;
2. If it requires parameters, then use a UDF, else use a view.

Razvan
AddThis Social Bookmark Button