2

Can you tell me what is the need for a stored procedure when there is UDF?

5 Answers 5

2

Off the top of my head, a stored procedure can do the following that a UDF cannot do:

1) Modify Data

2) Return a result set to a client

3) Perform non-deterministic activity

Sign up to request clarification or add additional context in comments.

4 Comments

A UDF can return a result set and perform non-deterministic activity too. It would be a table-valued function and a non-deterministic function, respectively :)
What he meant was UDFs cannot use non-deterministic functions like GetDate() etc.
If that is, in fact, what he meant, then he's still wrong. The only restriction that I am aware of regarding non-deterministic functions is that a function that uses them cannot be used in the context of a calculated column.
@Daniel: a non-deterministic function can be used in a computed column. Such a column, however, cannot be indexed.
1

A procedure can run DML, a function cannot.

In general, a function is designed to be used as a part of the query, while a stored procedure is a batch of SQL statements run together implementing some business logic, possibly with different credentials.

2 Comments

DDL is a bad idea even in a procedure.
I didn't say it was a good idea, I just said you can do it. ;)
1

A function cannot directly alter or update the database in any way, either via DML (INSERT, UPDATE, DELETE, ect) statements or DDL (CREATE TABLE, etc) statements. It also cannot do anything that might indirectly result in database modifications, such as executing ad-hoc SQL statements (of any sort) or executing stored procedures.

Comments

1

One key difference is that UDFs always have an output of fixed schema, stored procedures can result an arbitrary number of result sets in an arbitrary format.

Comments

0

Sprocs are used to return output to an application. A UDF returns a table variable.

1 Comment

No. A UDF can return a scalar result, or an inline query. A UDF does not have to return a table variable, and a Stored Procedure may return no output at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.