Can you tell me what is the need for a stored procedure when there is UDF?
5 Answers
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
4 Comments
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 :)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.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
Sprocs are used to return output to an application. A UDF returns a table variable.