all groups > sql server programming > october 2003 >
You're in the

sql server programming

group:

newbie: create transaction in udf


Re: newbie: create transaction in udf David Browne
10/27/2003 4:25:49 PM
sql server programming:
[quoted text, click to view]

Conceptually it is because UDF's are an extension to the query engine, not
stored procedures with return values.

Allowing DML inside a query is too ugly and difficult to even contemplate.

David

Re: newbie: create transaction in udf Aaron Bertrand - MVP
10/27/2003 5:06:51 PM
You can't have DDL/DML statements like those in a function (alter, drop,
create, update, insert, delete).

I think you meant to use a stored procedure, not a udf.




[quoted text, click to view]

Re: newbie: create transaction in udf Aaron Bertrand - MVP
10/27/2003 5:33:45 PM
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]

newbie: create transaction in udf Jeff
10/27/2003 10:55:19 PM
sqlserver2000/win2k3

Why are these statements:
BEGIN TRANSACTION
ROLLBACK TRANSACTION
UPDATE (to a table created outside of the function)
COMMIT TRANSACTION

not allowed in a function?

Jeff



Re: newbie: create transaction in udf Jeff
10/27/2003 11:16:53 PM
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.

Jeff



[quoted text, click to view]

Re: newbie: create transaction in udf Jeff
10/28/2003 12:03:48 AM
Thank you for taking the time to answer me. I have learned by reading your
answer.

I'll read those topics in BOL tomorrow
(it's 12pm here in norway, and I really have to go to sleep (log off)).






[quoted text, click to view]

Re: newbie: create transaction in udf Jeff
10/28/2003 12:06:44 AM
Ok

"David Browne" <davidbaxterbrowne no potted meat@hotmail.com> wrote in
message news:O5nlHlNnDHA.360@TK2MSFTNGP12.phx.gbl...
[quoted text, click to view]

Re: newbie: create transaction in udf Tibor Karaszi
10/28/2003 1:10:34 PM
[quoted text, click to view]

I agree that it might be some technical reason for this. However, IMO it is not that important
whether such exist or not. Below is enough for me:

SELECT MyFunc(col1) FROM tbl

Imagine some modification being performed in the function above. I can't even start thinking about
how that would work or how that would be perceived by the user.
"Did you modify any data?"
"No, honestly, I only did a SELECT..."
"Smack!!!"
;-)
--
Tibor Karaszi, SQL Server MVP
Archive at: http://groups.google.com/groups?oi=djq&as_ugroup=microsoft.public.sqlserver


[quoted text, click to view]

Re: newbie: create transaction in udf Louis Davidson
10/28/2003 4:06:11 PM
[quoted text, click to view]

What? Are you okay? The difference it makes is that we are programmers,
techies, people who as children were not happy until we had adjusted the TV
until the knobs fell off and the antennas broke. This so we could rip the
covers off and ask, how can I make it better, what makes it tick? If we
just blindly let things like this go, they could do whatever they wanted to
us. The more you understand the architecture of computers, the better a
programmer you are. Do you understand how indexes are structured? Or do
you you just know that they help you access data faster.

Anyhow, if no one cared HOW stuff worked, Kalen would be out of business
with her Inside SQL Server 2000 books, and you wouldn't want that :)


--
----------------------------------------------------------------------------
-----------
Louis Davidson (drsql@hotmail.com)
Compass Technology Management

Pro SQL Server 2000 Database Design
http://www.apress.com/book/bookDisplay.html?bID=266

Note: Please reply to the newsgroups only unless you are
interested in consulting services. All other replies will be ignored :)

AddThis Social Bookmark Button