|
Q1) why dml operations are not allowed in user defined functions for database tables? Q2) Why cannot we have non deterministic functions e.g. getdate() in UDF? Q3) Why functions cannot use ntext, image and timestamp data types as return type? Q4) Why cannot a function have output parameter? Q5) Why we cannot use Try..Catch block inside a function? Q6) Why Temporary table (#temp) and Derived table cannot be created on function? Q7) Why cannot we use print statement in udf? Q8)We cannot call stored procedures(exception for Extentended stored procedures ) from udf. Why? Thanks in advance...
(comments are locked)
|
|
Hi, Here's my attempt to answer your questions.
EDIT I forgot to post the example of derived tables in UDF. Here it goes. /EDIT The bottom line in it all is that SQL Server does not want to allow any scenarios where "wrong" results are possibly created.
Jun 29 '11 at 05:52 AM
Blackhawk-17
@Magnus Ahlkvist should get +8 for the litany of answers
Jun 29 '11 at 10:46 PM
KenJ
Also by using Synonym, we can use temporary table in function http://www.sqlservercentral.com/articles/UDF/74231/
Jul 21 '11 at 04:12 AM
learner
(comments are locked)
|


Let me add a comment to @Magnus Ahlkvist's excellent answer. UDFs are a very limited tool. They look attractive at first because in theory it allows you to encapsulate your code. But they really don't function very well. Scalar UDFs can effectively become a cursor when applied to your queries. Table Valued UDFs can work relatively well if they're not overly complex, but people tend to nest them (have one function call another, call another, etc.) or JOIN them, and this leads to major issues. Multi-Statement Table Valued UDFs have no statistics so they only work well with really small data sets (say, less than 100), and perform extremely poorly with larger data sets and suffer very badly when you nest them or JOIN them (worse than the Table Valued UDFs).
All that said, what are you trying to do with UDFs that you need all that functionality? I'd suggest you should stick with stored procedures.