Functions are used to *RETURN* data (either a scalar or a table), not to
*AFFECT* data. As stated in the topic CREATE FUNCTION in Books Online,
"Creates a user-defined function, which is a saved Transact-SQL routine that
returns a value. User-defined functions cannot be used to perform a set of
actions that modify the global database state." Also, see the topic
"User-Defined Functions," which states:
"The statements in a BEGIN...END block cannot have any side effects.
Function side effects are any permanent changes to the state of a resource
that has a scope outside the function such as a modification to a database
table. The only changes that can be made by the statements in the function
are changes to objects local to the function, such as local cursors or
variables. Modifications to database tables, operations on cursors that are
not local to the function, sending e-mail, attempting a catalog
modification, and generating a result set that is returned to the user are
examples of actions that cannot be performed in a function."
I guess I'm curious what difference it makes what are the fundamental
reasons behind this. Perhaps there is some code deep inside SQL Server that
is used to make pancakes, and a side effect of executing such a statement in
a function produces ketchup, and the SQL Server team does not like ketchup
on their pancakes. The point is, they are not allowed.
[quoted text, click to view] "Jeff" <it_consultant1@hotmail.com.NO_SPAM> wrote in message
news:ukP1JgNnDHA.3316@tk2msftngp13.phx.gbl...
> I know those statements are not allowed in a function, but don't know why
> they are not allowed
>
> I know stored procedures works fine with this.
>
> So, i'm wondering why those statements are not allowed in a function.